Be a Supporter!

Regular Expression Problem

  • 350 Views
  • 3 Replies
New Topic Respond to this Topic
Cyclonic
Cyclonic
  • Member since: May. 2, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Regular Expression Problem 2009-08-27 16:50:50 Reply

I am using highlight.js to do my syntax highlighting for my site, and to work, it needs escaped code in between a pre and a code tag. What I am doing to make sure that the code is escaped is running a regular expression replace function that replaces text between the pre and code tags with escaped text between pre and code tags. My PHP looks like this:

preg_replace("/<pre>&lt;code>(.*?)<\/code><\/pre>/isUe","'<pre>&lt;code>'.htmlspecialchars('$1').'&lt;/code></pre>'",$post);

I have tested it, and it doesn't match any of my code blocks (usually the code blocks are HTML snippits that are unescaped). What is the problem with that regular expression? It should work fine, right?

Cyclonic
Cyclonic
  • Member since: May. 2, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Regular Expression Problem 2009-08-27 16:52:36 Reply

Sorry for the double post, but newgrounds failed to unescape my &lt; characters. They are not actually there; I only put them in to satisfy the html formatting rules for BBS posts.

fourthfrench
fourthfrench
  • Member since: Aug. 13, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Regular Expression Problem 2009-08-27 21:01:31 Reply

This worked for me.

$post = "<pre><cod><p>some code</p></cod></pre>";
	$post = preg_replace("/<pre>\s*<cod>(.*)<\/cod>\s*<\/pre>/isUe",
						 "'<pre><cod>'.htmlspecialchars('$1').'</cod></pre>'",
						 $post);
	echo $post;

I left the e off of all code tags.

Cyclonic
Cyclonic
  • Member since: May. 2, 2009
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to Regular Expression Problem 2009-08-28 15:31:08 Reply

Hey thanks, I realized I left out the "$post = ", so it was replacing the code, and then not storing in a variable. A dumb mistake, for sure!