kaje Posted March 14, 2009 Share Posted March 14, 2009 I have a PHP page setup to parse a table from another website. Inside of that table are links to other pages. I want to change part of that URL to a different page. Example: Replace SportSelect.dbml?SPSID=1421 with roster.php?SPSID=1421 Here is my code that leads up to it: <?php include_once('simple_html_dom.php'); $sport = $_GET['SPID']; $page = $_GET['SPSID']; $sort = $_GET['SORT_ORDER']; $contents = file_get_html('http://www.url.com/'); echo $contents->find('table', 7)->innertext; I'd like the echo $contents->find('table', 7)->innertext; output to replace all links that contain the SportSelect.dbml with roster.php in the table. Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 14, 2009 Share Posted March 14, 2009 If it is just for this specific example, you could use str_replace(), otherwise, look into regular expressions and preg_replace(). Quote Link to comment Share on other sites More sharing options...
kaje Posted March 14, 2009 Author Share Posted March 14, 2009 If it is just for this specific example, you could use str_replace(), otherwise, look into regular expressions and preg_replace(). I've tried: $contents = file_get_html('http://www.url.com/'); $contents = str_replace("SportsSelect.dbml", "roster.php", $contents); echo $contents->find('table', 7)->innertext; before and it doesn't import the table at all. Just a blank page with the bgcolor. I'd prefer to not have to use regular expressions because it looks like an alien language to me. Quote Link to comment Share on other sites More sharing options...
zq29 Posted March 14, 2009 Share Posted March 14, 2009 Have you tried it the other way around? <?php $contents = file_get_html('http://www.url.com/'); $foo = $contents->find('table', 7)->innertext; $bar = str_replace("SportsSelect.dbml", "roster.php", $foo); echo $bar; ?> Quote Link to comment Share on other sites More sharing options...
alphanumetrix Posted March 14, 2009 Share Posted March 14, 2009 this should work: $string = 'whatever is something'; $pattern = '/something/'; $replacement = '<a href="#">something</a>'; $go = preg_replace($pattern, $replacement, $string); echo $go; that should replace "something" with "<a href="#">something</a>" Quote Link to comment Share on other sites More sharing options...
kaje Posted March 15, 2009 Author Share Posted March 15, 2009 Have you tried it the other way around? <?php $contents = file_get_html('http://www.url.com/'); $foo = $contents->find('table', 7)->innertext; $bar = str_replace("SportsSelect.dbml", "roster.php", $foo); echo $bar; ?> I just tried it the other way around and it showed the table, but it didn't change the links. Quote Link to comment Share on other sites More sharing options...
kaje Posted March 15, 2009 Author Share Posted March 15, 2009 this should work: $string = 'whatever is something'; $pattern = '/something/'; $replacement = '<a href="#">something</a>'; $go = preg_replace($pattern, $replacement, $string); echo $go; that should replace "something" with "<a href="#">something</a>" I'm wanting to change something that is already a link. Wanting to change the first of the link to something else so that it will go to my php page rather than look for a dbml file as it is on the source's site. Quote Link to comment Share on other sites More sharing options...
kaje Posted March 15, 2009 Author Share Posted March 15, 2009 Have you tried it the other way around? <?php $contents = file_get_html('http://www.url.com/'); $foo = $contents->find('table', 7)->innertext; $bar = str_replace("SportsSelect.dbml", "roster.php", $foo); echo $bar; ?> Don't mind the village idiot. I had a typo. This ended up working. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.