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