Jump to content


Photo

Regular Expressions


  • Please log in to reply
6 replies to this topic

#1 whatabrother

whatabrother
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 03:27 AM

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?




#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 October 2006 - 04:38 AM

You need a semicolon after $variable = 'hello', and you need double quotes for $pattern = '/$variable/'; instead of single quotes so $variable will interpolate.
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#3 whatabrother

whatabrother
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 06:01 AM

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\=".*."/


       


#4 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 October 2006 - 02:37 PM

Quantifiers are greedy by default; place a ? after them to make them ungreedy: /(?<=href=")(.*?)(?=")/
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#5 whatabrother

whatabrother
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 04:00 PM

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 )

#6 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 06 October 2006 - 04:10 PM

I don't know what your input is. Do you really want preg_match_all?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/

#7 whatabrother

whatabrother
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 October 2006 - 04:40 PM

yep, preg_match_all did it for me.  It always feels great to spend hours working with the wrong function! ha.

Thanks a ton




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users