Jump to content

[SOLVED] Replace Part of Text in Links


kaje

Recommended Posts

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

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.

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>"

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.

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.