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. :)

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

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+/?

Link to comment
Share on other sites

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.

 

 

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.