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

Link to comment
Share on other sites

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.

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.