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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.