whatabrother Posted October 6, 2006 Share Posted October 6, 2006 I need to use a variable in a replacement.i've been using this format:$string = 'xhellox';$variable = 'hello'$pattern = '/$variable/';$replacement = 'goodbye';$string = preg_replace($pattern,$replacement,$string,-1,$x);I would want for xhellox to turn in to xgoodbyex, but no replacements are made, and I am assuming that it is because I used a variable in the regular expression. What would be the proper way to do this? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 6, 2006 Share Posted October 6, 2006 You need a semicolon after[tt] $variable = 'hello'[/tt], and you need double quotes for [tt]$pattern = '/$variable/';[/tt] instead of single quotes so[tt] $variable [/tt]will interpolate. Quote Link to comment Share on other sites More sharing options...
whatabrother Posted October 6, 2006 Author Share Posted October 6, 2006 Thanks, that helped, but I ran in to another problem. I want to match everything between href=", and ". I used this expression, but it takes everything between the first double quote and the last double quote. I want it to match everything between href=" and the next double quote. This is the expression I've been trying to make work:/href\=".*."/ Quote Link to comment Share on other sites More sharing options...
effigy Posted October 6, 2006 Share Posted October 6, 2006 Quantifiers are greedy by default; place a[tt] ? [/tt]after them to make them ungreedy:[tt] /(?<=href=")(.*?)(?=")/[/tt] Quote Link to comment Share on other sites More sharing options...
whatabrother Posted October 6, 2006 Author Share Posted October 6, 2006 It worked better after using the expression that you posted, but it not quite as I expected. Here is my actual code:<?php//all my precreated functions. I have two in there that I use encrypt_data() and decrypt_data()require "vigilsreign/functions.php";$string= '<a href="http://example.com">example</a><a href="http://example2.com">example</a>';$pattern = '/(?<=href=")(.*?)(?=")/';preg_match($pattern,$string,$match);$y = 0;while ($match[$y]) { $pattern = '/href\="/'; $replacement = ''; $match[$y] = preg_replace($pattern,$replacement,$match[$y]); $pattern = '/"/'; $replacement = ''; $match[$y] = preg_replace($pattern,$replacement,$match[$y]); $url = parse_url($match[$y]); if (!isset($url[scheme])) { $url[scheme] = "http"; } if (!isset($url[host])) { $nothing = decrypt_data($_GET[variable]); $hmmm = parse_url($nothing); $url[host] = $hmmm[host]; } $hmmm = "$url[scheme]://$url[host]$url[path]"; $hmmm = encrypt_data($hmmm); $pattern = "/http:\/\/$url[host]/"; $replacement = "vigilsreign/proxy.php?page=1&&variable=$hmmm"; $string = preg_replace($pattern,$replacement,$string); ++$y; }?>the array $matches show's like this:Array ( [0] => http://myspace.com [1] => http://myspace.com )when I want it to show like this:Array ( [0] => http://example.com [1] => http://example2.com ) Quote Link to comment Share on other sites More sharing options...
effigy Posted October 6, 2006 Share Posted October 6, 2006 I don't know what your input is. Do you really want[tt] preg_match_all[/tt]? Quote Link to comment Share on other sites More sharing options...
whatabrother Posted October 6, 2006 Author Share Posted October 6, 2006 yep, preg_match_all did it for me. It always feels great to spend hours working with the wrong function! ha.Thanks a ton Quote Link to comment 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.