zero-perfoliate
zero-perfoliate

Author Topic: str_replace help  (Read 341 times)

Offline 57str@t

  • New PHP Members
  • Posts: 2
  • Karma: +0/-0
str_replace help
« on: December 02, 2009, 12:45:25 PM »
Hi there,

I am using an open source cms. The cms has a feature "Data sets" that include simple text areas ( no text editor ) but when characters like "&" are used, the page actually prints "&" among other invalid characters, so the page won't validate... so looking through the php files I cam across this ( I am not a PHP programmer )

Code: [Select]
function autop($pee, $br = 1) {
      $pee = $pee . "\n"; // just to make things a little easier, pad the end
      $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
      // Space things out a little
      $allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
      $pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
      $pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
      $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
      $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
      $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
      $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
      $pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
      $pee = preg_replace( '|<p>|', "$1<p>", $pee );
      $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
      $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
      $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
      $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
      $pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
      $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
      if ($br) {
         $pee = preg_replace('/<(script|style).*?<\/\\1>/se', 'str_replace("\n", "<WPPreserveNewline />", "\\0")', $pee);
         $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
         $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
      }
      $pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
      $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
      if (strpos($pee, '<pre') !== false)
         $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' ", $pee);
      $pee = preg_replace( "|\n</p>$|", '</p>', $pee );
      $pee = str_replace('&', '&amp;', $pee);
      $pee = str_replace('"', '&quot;', $pee);

      return $pee;
   }

I added the:


      $pee = str_replace('&', '&amp;', $pee);
      $pee = str_replace('"', '&quot;', $pee);

at the end, so the page now prints a "&amp;" when "&" is inputted in the text area, but it prints the &amp; in paragraphs, but wherever i have a blockquote, the "&" isn't changed to "&amp:"

So if there is something like this:

I type in:

Code: [Select]
<p>This is a paragraph to test & test</p>

The page now prints

<p>This is a paragraph to test &amp; test</p>

That's good buuuuut, if I type in the text area the following:

Code: [Select]
<blockquote><p>This is a paragraph to test & test</p></blockquote>

The page still prints

<blockquote><p>This is a paragraph to test & test</p></blockquote>

The "&" isn't changed like in the simple paragraph... I need the function to change the "&" between the blockquote tags...

Any insight would be appreciated!

hotnoob

  • Guest
Re: str_replace help
« Reply #1 on: December 02, 2009, 01:56:59 PM »
Rather than using the str_replace function, try using the htmlentities function.

So...

$pee = htmlentities($pee);

return $pee

if your still having problems, than, you should look what happens after the data is processed through the autop function.

Offline 57str@t

  • New PHP Members
  • Posts: 2
  • Karma: +0/-0
Re: str_replace help
« Reply #2 on: December 02, 2009, 09:35:57 PM »
Hi HN and thanks,

I'm not sure how to utilize that function, because this is a full blown cms, and I'm not really a PHP guy ( front end mainly ) so I am just trying to fix this one little annoyance... Everything works ok, but I am a validation nut ( have to validate strict doctype ) and just want to fix this one thing...

 

zero-perfoliate