Jump to content

Using preg_replace to replace contents into 2 locations


pandyboy

Recommended Posts

Hi - I have (for example) the following in a field in my database...

 

[[#201001251351#]]Some text here[[end]]

Something else in this bit

[[#201001251353#]]Some different text here[[end]]

And more different bits

[[#201001251357#]]Some more here[[end]]

Further different bits which may include other replaced things

 

I need it to display as the following:

<a href="javascript:;" onclick="Show_Stuff('201001251351');\">Show</a>
<div style="display:none;" id="201001251351">Some text here</div><br />
Something else in this bit
<a href="javascript:;" onclick="Show_Stuff('201001251353');\">Show</a>
<div style="display:none;" id="201001251353">Some different text here</div><br />
And more different bits
<a href="javascript:;" onclick="Show_Stuff('201001251357');\">Show</a>
<div style="display:none;" id="201001251357">Some more text here</div><br />
Further different bits which may include other replaced things

 

This would be easy if I only needed to use the time value once in each replacement, but as you will see I use it twice each time and I can't figure out how to duplicate it.

 

So in a nutshell I need to extract the bit between the [[# and #]] as a variable, then add it into both the javascript variable in the a tag and the id of the div tag and put everything between the #]] and the[[end]] in between the 2 div tags.

 

Hope this makes sense! Any help much appreciated.

 

I will probably kick myself later, as it'll no doubt be a blatant solution!

OK, I think I am half way there now.

 

I have adapted an example of preg_replace_callback from the PHP website, which gets me the value i need in 2 places, but I am having to use normal str_replace to finish the job, which seems a messy way to do it, as follows:

 

$input = "[[#201001251351#]]Some text here[[end]]
Something else in this bit
[[#201001251353#]]Some different text here[[end]]
And more different bits
[[#201001251357#]]Some more here[[end]]
Further different bits which may include other replaced things";

function parseTagsRecursive($input)
{

    $regex = '#\[\[\#((?:[^[]|(?!/?\#]])|(?R))+)\#]]#';

    if (is_array($input)) {
        $input = "<a href=\"javascript:;\" onclick=\"Show_Stuff('".$input[1]."');\">Vars</a>
<div style=\"display:none;\" id=\"".$input[1]."\">";
    }

    return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}

$output = parseTagsRecursive($input);

echo str_replace("[[end]]","</div>",$output);

 

This gives the desired output, but can anyone improve on this?

I would just go with a simple

 

<?php
$str = '[[#201001251351#]]Some text here[[end]]
Something else in this bit
[[#201001251353#]]Some different text here[[end]]
And more different bits
[[#201001251357#]]Some more here[[end]]
Further different bits which may include other replaced things';
$replace = '<a href="javascript:;" onclick="Show_Stuff(\'$1\');">Show</a>
<div style="display:none;" id="$1">$2</div><br />';
echo preg_replace('~\[\[#([0-9]+)#\]\](.*?)\[\[end\]\]~is', $replace, $str);
?>

Thanks - much better!

 

I really must get my head round regex!!

 

If I wanted to add in 2 more replacements, for example replace [* & *] with <b> & </b> and [~ & ~] with <i> & </i>, could I do these with the same line of code, or would it require a subsequent preg_replace?

No worries. In that case you can define an array of patterns and replacements:

 

$replace = array(
'~\[\[#([0-9]+)#\]\](.*?)\[\[end\]\]~is' => '<a href="javascript:;" onclick="Show_Stuff(\'$1\');">Show</a>
<div style="display:none;" id="$1">$2</div><br />',
'~\[\*(.*?)\*\]~s' => '<strong>$1</strong>',
'#\[~(.*?)~\]#s' => '<em>$1</em>'
);
$str = preg_replace(array_keys($replace), $replace, $str);

I used strong and em tags since the b and i tags are deprecated.

  • 2 weeks later...

Thanks - that works beautifully.

 

(By the way, B & I aren't deprecated. The difference between B & I and STRONG & EM is that the latter are used by screen readers to add strength or emphasis to words. As what I am doing is not ever intended for screen readers, but is purely visual, B & I are more appropriate)

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.