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. Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/ 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(). Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784780 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. Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784791 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; ?> Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784796 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>" Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784797 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. Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784971 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. Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784973 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! Link to comment https://forums.phpfreaks.com/topic/149421-solved-replace-part-of-text-in-links/#findComment-784985 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.