/    Sign up×
Community /Pin to ProfileBookmark

Replace Links in PHP

We currently have url’s that change all the time in our dynamic website (SEO reasons). To combat this, [U]most[/U] of the links in our pages are dynamic, meaning this is how we display an href:

[CODE]<a href=”<?php geeklink(45); ?>” target=”_blank”> …[/CODE]

“geeklink” is a function that simply pulls the corresponding value “logo-and-web-design-services” from the db as the page loads. Long story short, it works but this gets kind of messy in the back-end.

We would [B]instead[/B] like to have the following href do the same thing:

[CODE]<a href=”45″ target=”_blank”> …[/CODE]

[B]Primary Question:[/B] If all the web page body is stored in the string [B]$web_site_body[/B], then what would be the [U]most efficient method[/U] to search this string for all href tags, find the value of the href and if the condition is true, replace it with the corresponding value?

Thanks! ?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@lukazNov 04.2009 — [code=php]for($i=0;$i<=/*your max int*/;$i++){
$web_site_body = str_replace('href="'.$i.'"',/*whatever you want to replace with*/,$web_site_body);
}[/code]

I think this would work.
Copy linkTweet thisAlerts:
@ErikAndreauthorNov 04.2009 — Thanks Lukaz,

This would work, but I don't know if this would be the most efficient method. If we had 10,000 pages, this would run 10,000 times, but only replacing 5-6 links.

I sort of want to capture the value in each href, [U]then[/U] validate it and if true, replace. So when the engine comes in and sees the first href value, it checks it and replaces it with correct value if condition is true, then goes to the next value (if it exists) and replaces it with the correct value if condition is true, etc...

Thoughts?
Copy linkTweet thisAlerts:
@NogDogNov 04.2009 — I for one am still a bit confused as to what you really want to do. I suspect preg_replace() or preg_replace_callback() might be what you're looking for, but that's only a guess without a better understanding of what the input is and what the expected output would be.
Copy linkTweet thisAlerts:
@lukazNov 04.2009 — What are the links for? the site navigation? if so, why not generate the whole navigation from an include? Are you hard coding numbers into each page for the href values? seems to me with your math for 10000 pages that would be 50-60k hardcoded values. Could you explain a little more what you aim to do. cheers
Copy linkTweet thisAlerts:
@ErikAndreauthorNov 04.2009 — The code is the result of a WYSIWYG editor. The client edits what he wants on the page in WYSIWYG and it instantly updates each page. Sort of like a custom-built CMS. It doesn't really handle any kind of server-side-code, only html. It would be nice to simply make a link with the url id (mysql id) instead of a static url or PHP code so the client has control of the url name without breaking links throughout the site.

To keep things efficient, all URL's are called from SQL and placed into an array with the id as they key and the url as the value. So when any link is called, it just calls the array without having to call another SQL repeatedly.

When the public page is called, all links are parsed instantly using the first code example from my first post, except for the editable text generated from the WYSIWYG editor (which can't handle php, hence the reason for this post). If I can get away with just having an url id as an href value, it would help simplify things.

My first thought was a loop for every url id to replace all href='' tags with that value, but then that didn't seem efficient enough.

I guess I am looking to see if there is a way to scan the string for the first href value, replace it if it matches an url id (otherwise leave it alone) and then move on the next href value, etc..
Copy linkTweet thisAlerts:
@lukazNov 04.2009 — [code=php]function replace($match){
$return_str = substr($match[0],0,6).'hello';
return $return_str;
}
$str = ' href="45" href="46"';
$str = preg_replace_callback('|href="([0-9]+)|','replace',$str);
echo $str;[/code]


but replace $str with $web_site_body and 'hello' with your value to replace the number with. $match[1] is the numeric value in the href tags.

hope this helps
Copy linkTweet thisAlerts:
@ErikAndreauthorNov 04.2009 — Lukaz, I think this is what I was looking for. I'll let you know.
Copy linkTweet thisAlerts:
@ErikAndreauthorNov 04.2009 — Lukaz, you are life saver. I added the conditional values in and it works like a charm.

Many thanks!
Copy linkTweet thisAlerts:
@lukazNov 04.2009 — No problem, glad it helps.
×

Success!

Help @ErikAndre 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.17,
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,
)...