Jump to content

Archived

This topic is now archived and is closed to further replies.

whatabrother

Regular Expressions

Recommended Posts

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?


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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\=".*."/


       

Share this post


Link to post
Share on other sites
Quantifiers are greedy by default; place a[tt] ? [/tt]after them to make them ungreedy:[tt] /(?<=href=")(.*?)(?=")/[/tt]

Share this post


Link to post
Share on other sites
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 )

Share this post


Link to post
Share on other sites
I don't know what your input is. Do you really want[tt] preg_match_all[/tt]?

Share this post


Link to post
Share on other sites
yep, preg_match_all did it for me.  It always feels great to spend hours working with the wrong function! ha.

Thanks a ton

Share this post


Link to post
Share on other sites

×

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.