1

I need go get the node author as plain text and the same goes for the body field. So that means NO html markup. I cant find the right answer on google for drupal fields.

<?php print $name); ?> 

gives it with html...

1 Answer 1

0

By the time that a print $name is available, you are already in a template or other late stage theme item, correct? This means the parts have already been passed through render methods to add all that html.

When I had a project that created pdf reports for download and printing, removing these items was important. I added a helper function to my site module. (every one of my sites receives a site_[name].module, but you could add this to your template.php if you want to keep this all in the theme)

define('UNWANTED_REPORT_TAGS', 'span/h1/h2/h3/h4/div/strong');
/*
 * helper function for removing html from report contents
 * Strip only selcted tags in THEME_preprocess_node function below
 * strip defined constant tags if none given
 * Growth order of O(n) where n is the number of tags, so defaults to O(7)
 */
function module_strip_only($str, $tags = NULL, $keep = TRUE) {
  if ($tags == NULL) {
    // load the defaults
    $tags = explode('/', UNWANTED_REPORT_TAGS);
  }
  if (!is_array($tags)) {
    // we were passed a string, check format and parse
    $tags = (strpos($str, '>') !== false ? explode('>', str_replace('<', '', $tags)) : array($tags));
    if (end($tags) == '') {
      array_pop($tags);
    }
  }
  foreach ($tags as $tag) {
    // tag by tag, strip our given string
    if( $keep ) {
      $str = preg_replace('#</?' . $tag . '[^>]*>#is', '', $str);
    } else {
      // we want the usual text, drop tags and what they wrap
      $cleaning = TRUE;
      while( $cleaning ) {
        // regex probably doesnt work here
        $clipFrom = strpos( $str, '<' . $tag );
        $clipTo = strpos( $str, '</' . $tag , $clipFrom );
        $clipTo = strpos( $str, '>' , $clipTo ); // find end of closing tag
        $str = substr($str, 0, $clipFrom) . substr( $str, $clipTo );
        if( substr_count ( $str, '<'. $tag) < 1 ) {
          $cleaning = FALSE;
        }
      }
    }
  }
  return $str;
}

The function will need an appropriate name for you, of course. Then in your template file your line would be <?php print html_strip_only($name); ?>

If you just want to strip all <> sets from the string, you can do that like this. <?php print preg_replace('</?[^>]*>', '', $name); ?> and I am dont think that / needs escaped, but if it does then it would be \/. The online test tools want it escaped.

$pattern = '/<\\/?[^>]*>/';
$replacement = '';
$subject = '<p class="updated icon-clock">Updated: 1/14/2016</p><h3 class="field-label"> Related businesses  </h3>';
echo preg_replace($pattern, $replacement, $subject, -1 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.