Jump to content


Photo

regex and preg_replace help


  • Please log in to reply
6 replies to this topic

#1 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 02 June 2006 - 12:56 AM

Ok my function is supposed to select all the text inbetween <pre> and </pre> and then apply htmlspecialchars to it. Its used for a tutorial system, but it is not working.

Here is what I have so far, the before and after arent used at the moment.

function tutorialIt($data) {
// Changes the <>"" to the html ones
    $before = array('<','>','"');
    $after = array('&lt;','&gt;','&quot;');
        
    $data = preg_replace("/<pre>(.*?)<\/pre>/", "stripslashes(htmlspecialchars($1))", $data);
    echo $data;
}

Help?

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 02:19 AM

You don't need regular expressions to do that. Something like this:

function tutorialIt($data) 
{
   $content = htmlspecialchars($data, ENT_NOQUOTES);
   echo '<pre>' . $content . '</pre>';
}

Here is what htmlspecialchars() does:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]'&' (ampersand) becomes '&amp;'
'"' (double quote) becomes '&quot;' when ENT_NOQUOTES is not set.
''' (single quote) becomes ''' only when ENT_QUOTES is set.
'<' (less than) becomes '&lt;'
'>' (greater than) becomes '&gt;' [/quote]

[a href=\"http://www.php.net/htmlspecialchars\" target=\"_blank\"]http://www.php.net/htmlspecialchars[/a]
~ D Kuang

#3 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 02 June 2006 - 02:34 AM

But its for this, [a href=\"http://www.mileswjohnson.com/tutorials/1/the-basics-of-xhtml/\" target=\"_blank\"]http://www.mileswjohnson.com/tutorials/1/t...asics-of-xhtml/[/a]

The whole tutorial is text, yet I want only the text inbetween the pre tags to be formatted.

View the source and you will see.

#4 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 02 June 2006 - 04:03 PM

Oh, sure:

<?php

$data = '<html>
<title>Test</title>

<span style="color: blue">TutoriaL</span>

<br /><br />
Writing bold text:
<pre>
<b>This is a test code</b>
</pre>
Simple Javascript alert:
<pre>
<script type="text/javascript">
  alert("Hello There!");
</script>
</pre>
';

function convertHTML($matches)
{
    $before = array('<','>','"');
    $after = array('&lt;','&gt;','&quot;');
    
   return '<pre>' . str_replace($before, $after, $matches[1]) . '</pre>';
}

function tutorialIt($data) {
    $data = preg_replace_callback("/<pre>(.*?)<\/pre>/s", 'convertHTML', $data);
    echo $data;
}

tutorialIt($data);

?>

Which outputs something like this (note that the forum may screw it):

<html>
<title>Test</title>

<span style="color: blue">TutoriaL</span>

<br /><br />
Writing bold text:
<pre>
&lt;b&gt;This is a test code&lt;/b&gt;
</pre>
Simple Javascript alert:
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
  alert(&quot;Hello There!&quot;);
&lt;/script&gt;
</pre>

~ D Kuang

#5 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 03 June 2006 - 03:36 AM

Thank you very much, it worked! <3

#6 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 03 June 2006 - 04:53 AM

Hmmm... It's the second time I see this...

What does "<3" means? Is it s smilie?
~ D Kuang

#7 tiki

tiki
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts

Posted 03 June 2006 - 08:08 AM

Its a heart, so I basically love you. Lol




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users