Jump to content

preg_replace to remove information and then replace existing information


Buzzman25

Recommended Posts

Hello everyone!

 

I've searched the site and found nothing (least nothing that I understood) about what I wanted. I know it can be done by using preg_replace() but unfortunately I've never used it and don't even know how to read it...I'm totally lost. :(

 

What I'm trying to do are two things. First I want to find a specific code and remove it:

 

 onClick="javascript: pageTracker._trackPageview(\'/outgoing/website.com\');"

 

Everything in the code above remains the same except for "website.com" that gets pulled from a database and is changed to reflect the correct web page that I'm tracking outgoing links to. I want to completely remove all of that code as I am allowing reposting of the HTML. It's just extra clutter that I don't need shown in the <textarea> box.

 

That and it will give me some preg_replace() knowledge. ;)

 

The second thing I want to do is change a set link into my own link, that way when people post on my website it will direct them to my store instead of the general site used to purchase tickets. I want to change this:

 

http://www.website.com/OrderSystem/web/eventviewqb.asp?Affilid=93&EventsID=36814&PGName=Some+One

 

to

 

http://www.website.com/OrderSystem/web/eventviewqb.asp?Affilid=3036&EventsID=36814&PGName=Some+One

 

Basically I want to change the affiliate ID from 93 (default) to 3036 (mine).

 

I know it's kind of intense but I like a challenge and I want to learn how to do groovy replacements / matches in the future. :)

<pre>
<?php
echo $data = <<<DATA
 onClick="javascript: pageTracker._trackPageview(\'/outgoing/website.com\');"
 http://www.website.com/OrderSystem/web/eventviewqb.asp?Affilid=93&EventsID=36814&PGName=Some+One
DATA;
echo '<hr>';
$data = preg_replace('#onClick="javascript: pageTracker\._trackPageview\(\\\\\'/outgoing/[^\\\\]+\\\\\'\);"#', '', $data);
### This may need to be more specific depending on the data set.
$data = preg_replace('/(?<=\?Affilid=)\d+/', '3036', $data);
echo $data;
?>
</pre>

Oh that's friggin' awesome!

 

That's SO much less code then I was trying to use. I can't wait to get back from this assignment I'm on to code it and try it out! :D

 

So the # at the beginning of the code means to search for that text? And then the [^\\\\] means to start there and the + makes it go till \\\\\');#?

 

What does the (?<= mean as well as the )\d+/?

The # is the delimiter for the first pattern. Everything between the delimiters is the pattern that is used. [^\\\\]+ is used to match one or more characters that are not a backslash, and \\\\\' is used to match a backslash and a single quote. The backslashes are excessive because they are processed by the string and then the regex engine. (?<=...) is a lookaround--a positive lookbehind--which only matches a position. \d+ matches one or more digits.

 

See the resources.

 

 

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.