i have a script which will fetch content from a website, what i wanna do is modify all that links. Suppose:

$html = str_get_html('<h2 class="r"><a class="l" href="http://www.example.com/2009/07/page.html" onmousedown="return curwt(this, 'http://www.example.com/2009/07/page.html')">SEO Result Boost <b> </b></a></h2>');

so, is it possible to modify or rewrite it in this way>

<h2 class="r"><a class="l" href="http://www.site.com?http://www.example.com/2009/07/page.html">SEO Result Boost <b> </b></a></h2>

I have read it's manual but can not understand how to figure it ( http://simplehtmldom.sourceforge.net/#fragment-12 )
Is It Possible, Any Idea?

Recommended Answers

All 5 Replies

Member Avatar for diafol

Sure it's possible. You can use js to do it after it's left the server or you can do it server-side with xml/dom/based functions - I'm assuming that the HTML parsing class you mention will do it OR you could use something like preg_replace/str_replace.

ok, this is why, while fetching content i wanna modify all those links URL address automatically, i think in this case regex will be a good choice but can not understand how to do it.. any idea or example? Thanks

here is more description about my concern: i am using http://sourceforge.net/projects/simplehtmldom/ to fetch content from a site which has several links, but i dont wanna put those links directly in my website. What i wanna do, put a redirect for those links, so if there a link path, suppose http://go.com and i wanna change it to www.mysite.com/visit.php?http://go.com i know how to get those urls in simple php dom, it is something like this: $html = str_get_html($result);
foreach($html->find('a') as $element) but cant understand how to change or modify all Links URL's Path automatically, need your help :) Thanks

Member Avatar for diafol
<?php
$str = '<h2 class="r"><a class="l" onmousedown="return curwt(this, \'http://www.example.com/2009/07/page.html\')" href="http://www.example.com/2009/07/page.html" >SEO Result Boost <b> </b></a></h2>';

function stripMouse($txt, $htmlentities=false){
    $ret = preg_replace("/\s*onmousedown=\"[^\"]*\"/","",$txt);
    return ($htmlentities) ? htmlentities($ret) : $ret; 
}


echo stripMouse($str);
echo stripMouse($str,true);

Works so far for me, but I'm no regicist. Anybody else with a better regex?

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.