[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Make Regex Get Inside HREF Tag

Posted on 2009-04-16
6
Medium Priority
?
634 Views
Last Modified: 2012-05-06
I have a regex (attached) that replaces href="http://www.emarketbuilders.com" and href="http://www.emarketbuilders.com/" with href="http://www.emarketbuilders.com/?uid=1&cid=1," for example.

I want the regex to also accept anything before the final quote in the href tag (a href="") and append the $uid and $cid variables.

Therefore, the regex should take these URLs:
http://www.emarketbuilders.com
http://www.emarketbuilders.com/
http://www.emarketbuilders.com/splash_page.php

And, append them, respectively:
http://www.emarketbuilders.com/?uid=1&cid=1
http://www.emarketbuilders.com/?uid=1&cid=1
http://www.emarketbuilders.com/splash_page.php?uid=1&cid=1

This change must only take place inside of link tags, such as a href.
<?php
 
$uid = 1;
 
$cid = 1;
 
$url = "<p>Therefore, the regex should take these URLs: <a href="http://www.emarketbuilders.com">URL 1</a> <a href="http://www.emarketbuilders.com/">URL 2</a><a href="http://www.emarketbuilders.com/splash_page.php">URL 3</a></p>
 
$result = preg_replace('|href="http://www.emarketbuilders.com/?"|', 'href="http://www.emarketbuilders.com/?uid='.$uid.'&cid='.$cid.'"', $url);
 
// And turn them into:
// http://www.emarketbuilders.com
// http://www.emarketbuilders.com/
// http://www.emarketbuilders.com/splash_page.php
 
echo $result;
 
?>

Open in new window

0
Comment
Question by:EMB01
  • 2
  • 2
  • 2
6 Comments
 
LVL 4

Expert Comment

by:phpmonkey
ID: 24159894
try this:
$result = preg_replace('/(?:href=")[^"]*(?=")/i', '$0?uid=1&cid=1', $url);

Open in new window

0
 
LVL 4

Accepted Solution

by:
phpmonkey earned 2000 total points
ID: 24159926
This is with vars filled in and urls having to match the domain:
$result = preg_replace('/(?:href="http:\/\/www.emarketbuilders.com)[^"]*(?=")/i', '$0?uid='.$uid.'&cid='.$cid, $url);

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24160832
Hello,

Use this:

$result = preg_replace('|www.emarketbuilders.com[a-zA-Z0-9._%&$#@?\/]*|', $myurl, $url);

Regards
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 14

Expert Comment

by:shobinsun
ID: 24160853
0
 
LVL 14

Author Closing Comment

by:EMB01
ID: 31571084
Thanks!
0
 
LVL 14

Author Comment

by:EMB01
ID: 24170902
Sorry, I didn't get a change to try the second solution. Though, it may have worked. The first seemed to work well enough (although, I also failed to try it in multiple scenarios, I only tried one $url).

Thanks for your help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 10 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question