Link to home
Create AccountLog in
Avatar of Fernanditos
Fernanditos

asked on

Replacing urls inside PHP string

Hi,

I have some HTML code stored in a PHP variable ( $matches[0]; ), see the live example here: http://www.guses.com/test/curl5.php

I need to replace all javascript urls by html urls, eg.

The strings:
<a class="datatableborder" href="javascript:openTickerPopup('SUTB','OTCBB');"><b>SUTB</b></a>

must be replaced by:
<a class="datatableborder" href="http://sitename.com/_charts.php?symbol=SUTB"><b>SUTB</b></a>

Please notice that the javascript parameters are alwasy differents, I only need to get the FIRST one given, here was "SUTB" to pass it in my new replacement url.

Is it possible to do this?

Please help me out with the right coding. I have attached my real sample code.

Thank you



<?php
$ch = curl_init();
$timeout = 10; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.microcapmarkets.com/market_stat.jsp?sParam=p_Gainers&market=OTCBB&compType=all');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);
$pattern = '/Symbols.*?>.*?movers_ext/s';
echo '<table width=100% height=100% cellspacing=0 cellpadding=0 bordercolor=#ffffff border=0><tbody><tr><td width=13% align=left class=datatableheader>';
preg_match($pattern, $contents, $matches);
echo $matches[0];
?>

Open in new window

Avatar of ollyatstithians
ollyatstithians
Flag of United Kingdom of Great Britain and Northern Ireland image

This should work.
<?php
$result = preg_replace(
    '%<a class="([^"]+)" href="javascript:openTickerPopup\(\'([^\']+)\'([^)]*)\);"><b>([^<]+)</b></a>%', 
    '<a class="$1" href="http://sitename.com/_charts.php?symbol=$2"><b>$4</b></a>', 
    $matches[0]);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ollyatstithians
ollyatstithians
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Fernanditos
Fernanditos

ASKER

Wow, this was awesome.