ianco Posted October 21, 2011 Share Posted October 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/ Share on other sites More sharing options...
Psycho Posted October 21, 2011 Share Posted October 21, 2011 << 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 Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281198 Share on other sites More sharing options...
ianco Posted October 21, 2011 Author Share Posted October 21, 2011 That's much simpler, Thanks a lot Ian Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281199 Share on other sites More sharing options...
ianco Posted October 22, 2011 Author Share Posted October 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281387 Share on other sites More sharing options...
Psycho Posted October 22, 2011 Share Posted October 22, 2011 That makes absolutely no sense. What is in "$content" and what are you trying to search for and what are you wanting to replace it with? Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281429 Share on other sites More sharing options...
ianco Posted October 23, 2011 Author Share Posted October 23, 2011 Sorry, I've just replaced input and output from the previous example above with content. Either way I want to reverse the first situation so that my HTML turns into short hand: {{http://en.wikipedia.org/wiki/File:Wiki.png||wikipedia logo||This is the wikipedia logo}} Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281490 Share on other sites More sharing options...
Psycho Posted October 23, 2011 Share Posted October 23, 2011 That is a much, much different situation. $regex = "#<figure><img src='([^']*)' alt='([^']*)' /><figcaption>([^']*)</figcaption></figure>#"; $replace = "{{$1||$2||$3}}"; $output = preg_replace($regex, $replace, $input); Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1281621 Share on other sites More sharing options...
ianco Posted October 25, 2011 Author Share Posted October 25, 2011 for some reason $replace = "{{$1||$2||$3}}"; is killing my script. I just get a blank page unless I delete it. Why would this be?? Ian Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1282168 Share on other sites More sharing options...
requinix Posted October 25, 2011 Share Posted October 25, 2011 "{$" is interpreted as the start of a variable. Which, in this case, it is not. Thus PHP barfs. Use single quotes or escape the $. Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1282186 Share on other sites More sharing options...
ianco Posted November 4, 2011 Author Share Posted November 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285025 Share on other sites More sharing options...
Psycho Posted November 4, 2011 Share Posted November 4, 2011 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}}"; Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285044 Share on other sites More sharing options...
ianco Posted November 5, 2011 Author Share Posted November 5, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285249 Share on other sites More sharing options...
Psycho Posted November 6, 2011 Share Posted November 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249549-preg_replace-woes/#findComment-1285418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.