Buzzman25 Posted May 1, 2008 Share Posted May 1, 2008 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 https://forums.phpfreaks.com/topic/103683-preg_replace-to-remove-information-and-then-replace-existing-information/ Share on other sites More sharing options...
effigy Posted May 1, 2008 Share Posted May 1, 2008 <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 https://forums.phpfreaks.com/topic/103683-preg_replace-to-remove-information-and-then-replace-existing-information/#findComment-531046 Share on other sites More sharing options...
Buzzman25 Posted May 3, 2008 Author Share Posted May 3, 2008 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! 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 https://forums.phpfreaks.com/topic/103683-preg_replace-to-remove-information-and-then-replace-existing-information/#findComment-532305 Share on other sites More sharing options...
effigy Posted May 5, 2008 Share Posted May 5, 2008 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 https://forums.phpfreaks.com/topic/103683-preg_replace-to-remove-information-and-then-replace-existing-information/#findComment-533476 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.