Jump to content

str_replace help


57str@t

Recommended Posts

Hey there,

 

I'm a PHP newb, I'm having troubles with the str_replace function. I want to clean up bad html to valid xhtml, I am using:

 

$pee = str_replace('&', '&', $pee);

 

It works, but doesn't change & to & when the content is inside of <blockquote><p>stuff here & there</p></blockquote>

 

can anyone assist?

Link to comment
https://forums.phpfreaks.com/topic/183066-str_replace-help/
Share on other sites

Hi there and thanks for the reply,

 

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

 

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('&', '&', $pee);

$pee = str_replace('"', '"', $pee);

 

return $pee;

}

 

I added the:

 

 

$pee = str_replace('&', '&', $pee);

$pee = str_replace('"', '"', $pee);

 

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

 

So if there is something like this:

 

I type in:

 

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

 

The page now prints

 

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

 

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

 

<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...

 

Thanks sorry if i sound so naive, I'm a front end guy, and don't code PHP too often..

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/183066-str_replace-help/#findComment-966258
Share on other sites

Have you ever considered using htmlentities with ENT_COMPAT parameter? It's infinitely easier and only converts double quotes and &, and alike.

 

echo htmlentities('<blockquote><p>This is a paragraph to test & test. Quotes: "" " """</p></blockquote>', ENT_COMPAT);

Link to comment
https://forums.phpfreaks.com/topic/183066-str_replace-help/#findComment-966817
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.