Jump to content

[SOLVED] regular expressions problem


AviNahum

Recommended Posts

hey,

i'm trying to convert some special combination to images...

 

it's my first time i'm try to use regular expressions...

this is the function i using:

public function convert($txt) {
global $DB;

$txt = preg_replace( "<{img_(.+?)\}>", "<img src='\\1\.gif'>", $txt );

return $txt;
}

 

in simple words, i want it to convert the combination <{img_1}> to <img src='1.gif'>

the number 1 (<{img_1}> ) can change to any number...

this function wont work and shows me an error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in C:\wamp\www\cms\functions.php on line 260

 

Thanks!

 

***sorry for poor english***

Link to comment
https://forums.phpfreaks.com/topic/165087-solved-regular-expressions-problem/
Share on other sites

thanks for trying to help,

but i already tried this tutorial and i cant make it to work

 

If you already tried that tutorial then you would know that you need to implement delimiters.  Please read through it again or Google, I have already told you what the cause of the error is.

i need some more help and i thought it's will be better if i post it here and dont open a new topic...

 

$img_id = preg_replace( "/<{img_(.+?)\}>/", "\\1", $txt );

 

this code works fine,

for example:

this:

<p>
sad <{img_2}> asd
</p>

 

will be replaced with:

<p>
sad 2 asd
</p>

 

i want to remove all characters  thats comes before and after the number "2"

i tried this code:

$img_id = preg_replace( "/(.+?)\/ /<{img_(.+?)\ /(.+?)\/}>/", "\\2", $txt );

 

but its wont work and shows this error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '&' in C:\wamp\www\cms\functions.php on line 265

 

i tried a few another ways but still it's wont work...

 

thanks!

Hmm.. Im not sure if I follow correctly.. do you mean something like this?

 

$txt = <<<EOD
<p>
sad <{img_2}> asd
</p>
EOD;

$img_id = preg_replace('#<{img_([^}]+)}>#', '\1', $txt );
echo $img_id; // output: sad 2 asd

For the capture, I use ([^}]+) instead of \d+, in case there can be anything as opposed to only digits. If it will in fact only be digits, you can use this instead: ([0-9]+) or (\d+) [\d assumes there is no locale / exponent issues].

assume that the var $txt contain this:

<p>
sad <{img_2}> asd
</p>

 

i want the output will be 2

i want all the content that comes before and after the number 2 will removed...

and its important to me to replace the "<" and ">" with "<" and ">"

 

i tried this:

$img_id = preg_replace("/^(.+?)\<{img_(.+?)\}>(.+?)\$/", "\\2", $txt );

 

but its still wont work...

 

and thanks for trying to help!

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.