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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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].

Link to comment
Share on other sites

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!

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.