Jump to content


Photo

REGEX help


  • Please log in to reply
4 replies to this topic

#1 sanjoyc

sanjoyc
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 August 2006 - 11:52 AM

Please help me in REGEX code. I am very new to REGEX. I have text like:
This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...
I need to replace all {{img with <img and }} with >

How could I do this? Anyone can help me?


#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 12:15 PM

Please help me in REGEX code. I am very new to REGEX. I have text like:

This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...
I need to replace all {{img with <img and }} with >

How could I do this? Anyone can help me?


try this:
<?php
$String = preg_replace('|\{\{img(.+?)\}\}|i', "<img$1 />", $String;
?>

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 sanjoyc

sanjoyc
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 25 August 2006 - 12:51 PM

Thanks for quick reply.

But my problem does not solve yet.

The text {{img= should be replaced with <img src and }} with >
So the following text

This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...

should be look like

This is an image <img src='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'> and this is another image <img src=='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'>...blah blah blah...


But when I used your code:

$str = "This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...";

$test = preg_replace('|\{\{img(.+?)\}\}|i', "<img$1 />", $str);

print_r( $test);

I got the result like

This is an image  and this is another image ...blah blah blah...


I think, I can clarify my problem now.

Thanks

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 25 August 2006 - 01:09 PM

actually, if you view your source of your page instead of just the output, you'll see that the code provided does exactly what you're after. the problem is that when you export it to the screen, the browser will interpret it as an image tag. if you're wanting to print the result to the screen, use htmlentities() on it first. try this:
<?php
$str = "This is an image {{img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text'}} and this is another image {{img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text'}}...blah blah blah...";

$test = htmlentities(preg_replace('|\{\{img(.+?)\}\}|i', "<img$1 />", $str));

print_r( $test);
?>

the result of that code is:

This is an image <img='http://path/to/image/imagename.jpg' border='0' width='100' alt='Alternate text' /> and this is another image <img='http://path/to/image/imagename.png' border='0' width='100' alt='Alternate text' />...blah blah blah...


You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 sanjoyc

sanjoyc
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 28 August 2006 - 02:34 PM

Many many thanks....




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users