Jump to content

preg_replace woes


ianco

Recommended Posts

Evening all,

 

I'm looking to use preg_replace to change:

 

{{http://en.wikipedia.org/wiki/File:Wiki.png||wikipedia logo||This is the wikipedia logo}}

 

into

 

<figure><img src='http://en.wikipedia.org/wiki/File:Wiki.png' alt='wikipedia logo' /><figcaption>This is the wikipedia logo</figcaption></figure>

 

for example

 

so far i have

 

	$befor = array(); 
$befor[1] = "/({{)(.*?)(|)/";
$befor[2] = "/(|)(.*?)(|)/";
$befor[3] = "/(|)(.*?)(}})/";


$aftr = array();
$aftr[1] = "<figure><img src='$1'";
$aftr[2] = " alt='$2' />";
$aftr[3] = "<figcaption>$3</figcaption></figure>";


$content = preg_replace($befor, $aftr, $content);	

 

I'm obviously way out because it's just spitting out

 

alt='' /> alt='<' /> alt='' /> alt='f' />...etc

 

can anyone help me out?

 

Many thanks

 

Ian

 

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/
Share on other sites

<< Moved to Regex Forum >>

 

$input = "some text before {{http://en.wikipedia.org/wiki/File:Wiki.png||wikipedia logo||This is the wikipedia logo}} some text after";

$regex = "#{{([^|]*)\|\|([^|]*)\|\|([^|]*)}}#";
$replace = "<figure><img src='$1' alt='$2' /><figcaption>$3</figcaption></figure>";
$output = preg_replace($regex, $replace, $input);

echo $output;
//Output:
//some text before <figure><img src='http://en.wikipedia.org/wiki/File:Wiki.png' alt='wikipedia logo' /><figcaption>This is the wikipedia logo</figcaption></figure> some text after

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281198
Share on other sites

I want to now reverse it. SO, I've got

 


$regex1 = '{{$1||$2||$3}}';								
$replace1 = '#<figure><img src="([^<]+)" alt="([^<]+)" /><figcaption>([^<]+)</figcaption></figure>#i';

$content = preg_replace($replace, $regex, $content);

 

but this isn't working. I must be near...

Any ideas?

 

Thanks

 

Ian

 

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281387
Share on other sites

  • 2 weeks later...

Hi Sorry to bang on about this but i'm still not having any luck with this.

 

$regex = "#<figure><img src='([^']*)' alt='([^']*)' /><figcaption>([^']*)</figcaption></figure>#";
$replace = "(($1**$2**$3))";
$pagecontent = preg_replace($regex, $replace, $pagecontent);

 

no curly brackets now and still won't preg_replace

 

any ideas?

 

Thanks folks

 

 

Ian

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285025
Share on other sites

Hi Sorry to bang on about this but i'm still not having any luck with this.

 

You were already given the solution by requinix:

Use single quotes or escape the $.

 

Use single quotes

$replace = '{{$1||$2||$3}}';

 

or escape the dollar sign

$replace = "{{\$1||\$2||\$3}}";

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285044
Share on other sites

I played about with it a bit more and found that in the $regex line i needed \ (slash) before the apostrophe.

 

so, here it is. thanks guys

 

$regex = '#<figure><img src="([^\']*)" alt="([^\']*)" /><figcaption>([^\']*)</figcaption></figure>#';
$replace = '{{$1||$2||$3}}';
$pagecontent = preg_replace($regex, $replace, $pagecontent);

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285249
Share on other sites

I played about with it a bit more and found that in the $regex line i needed \ (slash) before the apostrophe.

 

Well, if you would have followed directions you wouldn't have had that problem. The regex I supplied was defined using double quotes around the string for a reason - so you wouldn't need to escape the single quotes inside the string.

Link to comment
https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285418
Share on other sites

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.