Jump to content

regex and preg_replace help


tiki

Recommended Posts

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.

[code]
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;
}
[/code]

Help?
Link to comment
Share on other sites

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

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

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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Oh, sure:

[code]<?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);

?>[/code]

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

[code]<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>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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