/    Sign up×
Community /Pin to ProfileBookmark

Is there anyway (i know there is, or so Im pretty sure) to grab all the URLs in a $var, and change the URLs?

lol
thanks

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@SpectreReturnsJan 04.2006 — Erm, can you give us an example? Your post isn't very descriptive.
Copy linkTweet thisAlerts:
@scojo1Jan 04.2006 — [URL=http://us3.php.net/manual/en/function.preg-replace.php]http://us3.php.net/manual/en/function.preg-replace.php[/URL]


check out the link for the function preg_replace

it takes a pattern and replaces it, assuming you know the address you want to change you could do something like:

[code=php]
$new_var = preg_replace('/http:www.whatever.com/', $new_url, $var);
[/code]


Although like SpectreReturns said, you're a bit vague and I'm not sure if this is exatly what you want
Copy linkTweet thisAlerts:
@Kyleva2204authorJan 04.2006 — [URL=http://us3.php.net/manual/en/function.preg-replace.php]http://us3.php.net/manual/en/function.preg-replace.php[/URL]


check out the link for the function preg_replace

it takes a pattern and replaces it, assuming you know the address you want to change you could do something like:

[code=php]
$new_var = preg_replace('/http:www.whatever.com/', $new_url, $var);
[/code]


Although like SpectreReturns said, you're a bit vague and I'm not sure if this is exatly what you want[/QUOTE]

Ok, now.. sorry but is there anyway I can include the previous URL in that? :-
Copy linkTweet thisAlerts:
@acorbelliJan 04.2006 — I had some code here, but as I looked at it I realized I'm not quite sure what you're asking.

Could you please elaborate to what you want?
Copy linkTweet thisAlerts:
@Kyleva2204authorJan 04.2006 — Basically, I want to take all the URLs out of a page.. or Var. and replace them to something like. refer.com/?oldurl where oldurl is, it should be the old url.

sooo say this is the original document:

[CODE]<A href="http://me.com/downloads.php">CLICK MEEE!!</a>[/CODE]

then, the PHP takes the document and replaces anything it sees that has http:// or www. or .com in the name to replace it with

[CODE]<a href="http://mysite.com/url=me.com/downloads.php">CLICK MEEE!</a>[/CODE]

and I want it to do that throught the document.. or var. w/e.

Get it now? :-P

much thanks!
Copy linkTweet thisAlerts:
@JDM71488Jan 04.2006 — [B]well, if i understood correctly, and you want to redirect to pages on your website, then hopefully this is what you are looking for.[/B]

make all of your links point to a seperate page like redirect.php. then have this page redirect them to the value of the one sent in the page.

[code=html]<a href="http://www.yoursite.com/redirect.php?url=downloads.php">visit my downloads section</a>[/code]

&lt;?php
$goto = "http://www.yoursite.com/".$_GET['url'];

if( substr($goto, 0, 24) == 'http://www.yoursite.com/')
{
header("Location: $goto");
}
else
{
header("Location: http://www.yoursite.com/"); // goes to your home page
//header("Location: ".$_SERVER['HTTP_REFERER']); // goes to previous page
//die("an error has occured. please do not tamper with the urls.");
}
?&gt;

[B]otherwise, this is for external links.[/B]

[code=html]<a href="redirect.php?url=accessjdm.com">JDM's Website</a>[/code]

&lt;?php
$goto = "http://".$_GET['url'];
header("Location: $goto");
?&gt;
Copy linkTweet thisAlerts:
@Kyleva2204authorJan 04.2006 — hmm, not quite. Its not really a redirect.

ok this is what ive gatherd up for what i want.. I baisally want an EREG to scan the page, and grab everything it sees that has http:// & .com or one or the other.. and grab it and put it into a varible, then do a preg_replace on it, and replace it with a new url. :-... I just don't know how to make an ereg continue to scan the page.. after its found one hit.

this is what I got..

[code=php]ereg(""([^"]*).com", $data, $links);
$data = preg_replace($links[1], "http://mysite.com/file.php?url=$links[1]", $data);
[/code]


maybe that might help you in better understanding?

?

again

much thanks ?)
Copy linkTweet thisAlerts:
@acorbelliJan 04.2006 — Okay, so you grab the data from a file and then use a RegExp to find and replace all of the instances of every link with a link to a certain page. You've got the basic idea, so are you looking for the RegExp to do it, or the php code, or both?
Copy linkTweet thisAlerts:
@Kyleva2204authorJan 04.2006 — Okay, so you grab the data from a file and then use a RegExp to find and replace all of the instances of every link with a link to a certain page. You've got the basic idea, so are you looking for the RegExp to do it, or the php code, or both?[/QUOTE]
both ? lol

much thanks! ?

Kyle
Copy linkTweet thisAlerts:
@acorbelliJan 05.2006 — [code=php]
$data = "<a href="http://something.com">Something</a>n<br>n<a href="http://somethingelse.com">Something Else</a>n<br>"; // A sample data variable

$reg = "<a href="([^"><]+)">([^<>]+)</a>"; // Your RegExp

$replace = "<a href="http://whatyouwant.com">What You Want</a>"; // The code to the link you want to replace it with

$test = ereg_replace($reg, $replace, $data); // A call to ereg_replace(), you can use preg_replace if your server supports it
[/code]


Lemme know if that's what you need!
Copy linkTweet thisAlerts:
@ShrineDesignsJan 05.2006 — or[code=php]<?php
$data = '<a href="http://www.google.com">google</a>

<iframe src="http://domain.com/path/to/nowhere">
</iframe>

';
if(preg_match_all("/(?:href|src)=["']?([^"'<>]+)["']?/", $data, $m))
{
$m = $m[1];

foreach($m as $u)
{
$u = rawurlencode($u);
echo "<a href="redirect.php?url=$u">link</a><br>n";
}
}
?>[/code]
Copy linkTweet thisAlerts:
@acorbelliJan 05.2006 — Hey Shrine,

Out of sheer curiosity (for personal use) are there any advantages that your script has that mine doesn't?
Copy linkTweet thisAlerts:
@ShrineDesignsJan 05.2006 — it captures href or src attributes from any element, where yours only caputers the href from 'a' elements
Copy linkTweet thisAlerts:
@acorbelliJan 05.2006 — This is true (from looking at the regexp) but I meant more along the lines of the PHP in question, any speed differences...safety concerns, etc?

I believe the OP asked specifically about links, which is why mine is tailored to grab an "a" tag. The intent was to make it easier to read, and easier to debug if something strange should happen. I may be wrong though.
Copy linkTweet thisAlerts:
@ShrineDesignsJan 05.2006 — yours would probably be faster, mine uses two statements and one of which is a loop

php's built-in functions are really fast, because php is written in C++
Copy linkTweet thisAlerts:
@Kyleva2204authorJan 05.2006 — w00t thanks a-bunch :-D
×

Success!

Help @Kyleva2204 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...