Jump to content

Regular Expressions


whatabrother

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?


Link to comment
https://forums.phpfreaks.com/topic/23143-regular-expressions/
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\=".*."/


       
Link to comment
https://forums.phpfreaks.com/topic/23143-regular-expressions/#findComment-104813
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 )
Link to comment
https://forums.phpfreaks.com/topic/23143-regular-expressions/#findComment-105098
Share on other sites

Archived

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

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