Jump to content

str replace and re write


shaneg55

Recommended Posts

how can i find reference to  something like this "navigationID=3" or "navigationID=44" within a string and then re write it to become something like this "/en/content/3/Link_Name"?

 

there could be more than one reference within the string and the integer val (ie 3 and or 44) will not be known.

 

Any help with this would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/213013-str-replace-and-re-write/
Share on other sites

how can i find reference to  something like this "navigationID=3" or "navigationID=44" within a string and then re write it to become something like this "/en/content/3/Link_Name"?

 

there could be more than one reference within the string and the integer val (ie 3 and or 44) will not be known.

 

Any help with this would be greatly appreciated.

 

Regex would help, but if you want to use str_replace, thats fine.

 

$string = "blahblah navigationID=44";

$replacement = "/en/content/3/Link_Name";

echo str_replace("navigationID=44",$replacement,$string);

 

Atleast I think thats what you want...

I was thinking something more along the lines of this:

 

function getTextBetweenTags($string, $tagname, $lang) {
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();
    $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/";
    preg_match($pattern, $string, $matches);

$sql= "SELECT * FROM navigation, pages WHERE navigation.navigationID = '$matches[1]' AND navigation.pageID = pages.pageID";
$rows = $db->query($sql);
while ($record = $db->fetch_array($rows)) {
$newtext = "http://www.mydomain.com/".$lang."/".$record[navigationID]."/".str_replace(" ", "_", $record[linkname]);
}
return $newtext;
}
$str = '<p align="left">Go To This <a href="<navigation>3</navigation>">Page</a>.</p><p>This <a href="<navigation>4</navigation>">Page</a> is also good.</p>';
$txt = getTextBetweenTags($str, "navigation", "en");
echo $txt;

 

Which returns "http://mydomain.com/en/4/My_Link_Name"

 

I want it to return "Go To This Page. This Page is also good." With the words "Page" linked with the properly formatted url.

 

I think im missing incorporating an str_replace here somehow so i return the properly formatted urls and the rest of the string where applicable.  Hope im making some sort of sense here.

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.