Hi, What I want to do is to take a link like this http://test.com and make it into http.//www.mydomain.com/redirect.php?site=test.com I got my data in a string (they need to be submitted to a database). So I've tried using str_replace like this: $content2 = str_replace("http://", "http.//www.mydomain.com/redirect.php?mid={$msg_id}&site=", $content); PHP: however what I get from this is a link to http://localhost:8888/%22http://www.mydomain.com/redirect.php?site=test.com\%22 Code (markup): Which I really dont get (note I'm currently testing on my localhost..) Someone can help me get this done correctly? Thanks a lot Jacob / AzaraT
<?php $url = 'http://test.com'; preg_match('|^(?:http://)?(www\.)?([^/]+)|i', $url, $matches); $newURL = 'http.//www.mydomain.com/redirect.php?site=' . $matches[2]; echo $newURL; ?> PHP:
thanks, however it doesn't work. What I got now is this. $content = '<a href="http://test.com">test</a> <p> Other html code here blablabla </p>'; preg_match('|^(?:http://)?(www\.)?([^/]+)|i', $content, $matches); $newURL = 'http//www.mydomain.com/redirect.php?site=' . $matches[2]; echo $newURL; PHP: and it returns: http//www.mydomain.com/redirect.php?site= Code (markup): What it needed to return was: <a href="http//www.mydomain.com/redirect.php?site=test.com">test</a> <p> Other html code here blablabla </p> HTML:
<?php function callback($match) { return 'http://www.mydomain.com/redirect.php?site='.parse_url($match[1], PHP_URL_HOST); } $content = '<a href="http://test.com">test</a> <p> Other html code here blablabla </p>'; //this is where the changes take effect... $content = preg_replace_callback('~(http://[^ "]+)~i', 'callback', $content); echo $content; ?> PHP:
Hi again As said this worked, however there is one small issue. If I have this <a href="http://test.com">http://test.com</a> It also rewrites the text that is displayed, I only want it to change the href atrribute, and not other things. Eg if the content is: $content = '<a href="http://test.com">http://test.com</a> <p> Other html code here blablabla </p>'; PHP: it should return: <a href="http//www.mydomain.com/redirect.php?site=test.com">http://test.com</a> <p> Other html code here blablabla </p> HTML: Someone can help me with this? I really don't understand this :s eg all the special charectors in the preg_replace_callback Thanks
The way I would handle this is assign the clicked linked a variable such as below: <a href="http//www.mydomain.com/redirect.php?site=test.com" name="test">http://test.com</a> PHP: Then in this code: $test = $_GET['name']; function callback($match) { return 'http://www.mydomain.com/redirect.php?site='.parse_url($match[1], PHP_URL_HOST); } $content = '<a href="http://test.com">test</a> <p> Other html code here blablabla </p>'; //this is where the changes take effect... $content = preg_replace_callback('~(http://[^ "]+)~i', 'callback', $content); echo $content; ?> PHP: Notice I added at the top to call the variable since it can still be passed as data. This is untested as I haven't worked with passing data from a href to a referrer file.
change it like $content = preg_replace_callback('~href="(http://[^ "]+)~i', 'callback', $content); PHP:
@gapz101 That worked. I have already tired that but for some reason I used " instead of ' in the callback function and that seems to have bugged it or something. Weird But thanks