57str@t Posted November 26, 2009 Share Posted November 26, 2009 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? Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 26, 2009 Share Posted November 26, 2009 post your usage, ie., post an example of how you're using it in your actual code. Quote Link to comment Share on other sites More sharing options...
57str@t Posted November 27, 2009 Author Share Posted November 27, 2009 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*</?(?|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 "&:" 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.. Quote Link to comment Share on other sites More sharing options...
57str@t Posted November 28, 2009 Author Share Posted November 28, 2009 Anyone? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted November 28, 2009 Share Posted November 28, 2009 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); Quote Link to comment Share on other sites More sharing options...
57str@t Posted November 29, 2009 Author Share Posted November 29, 2009 I wouldn't know how to implement this... I am trying to modify an already existing script in an open source cms... I just need to modify to change "&" to "&" in between <blockquote> & </blockquote> tags... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.