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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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