View Full Version : Help with strings

03-11-2010, 03:07 AM
I have the follwing in a text string:


I'd like to be able to search this in a string and basically find the word/words inbetween <link> and <endlink> and make it an actual link, so:

<a href="construction.php">construction</a>

Also, there could be multiple of these in a string. Any ideas?? I also have the following code:

$txt = $resultsettxt['text'];
$newtxt = str_replace("<endbullet><bullet>", "</li><li>", $txt);
$newtxt = str_replace("<bullet>", "<li>", $txt);
$newtxt = str_replace("<endbullet>", "</li><br>", $newtxt);
$newtxt = str_replace("<bold>", "<strong>", $newtxt);
$newtxt = str_replace("<endbold>", "</strong>", $newtxt);
echo nl2br($newtxt);

Which works good and want to continue to use this so if there is a way to incorporate this with the <link> then I'm open to ideas.

Thanks in advance!

03-11-2010, 03:13 AM
Also, if there is anyway if it looks like this:


To only use the capitalized word in the word part of the link and make it lowercase in the URL part. So this would look like this:

<a href="construction.php">Construction</a>


03-11-2010, 02:11 PM
Any ideas on this?? Thanks!

03-11-2010, 02:13 PM
take a look at www.php.net/preg_replace

03-11-2010, 02:16 PM
I did something similar to that with the str_replace but don't know how to get the value(s) between the <link> and <endlink>

03-11-2010, 02:28 PM
Just guessing here, not tested

$newtext = preg_replace('/\<link\>(.*?)\<\/endlink\>/is','<a href="'.$1.'.html">'.ucfirst($1).'</a>',$txt);

03-11-2010, 02:29 PM
$txt = preg_replace('/\<link\>(.*?)\<endlink\>/', '<a href="\\1.php">\\1</a>', $txt);

The "\\1" part refers to the bracketed match; the "(.*?)".

The match says "zero or more instances of any character" the "?" makes it "ungreedy" so it doesn't break "<link>a<endlink> <link>b<endlink>" by matching "a<endlink> <link>b" instead of "a" and "b" separately.

This may well be over your head, in which case as soon as you try to make the matching more complex, say in order to support something like "<link title=foo's title>foo<endlink>" you'll run into big problems; I'd advise that you really look into learning about regular expressions in more depth.

03-11-2010, 02:56 PM
So I tried to do what you did with the following code:

$testtxt = "<link>Test<endlink>";
$testtxt = preg_replace('/\<link\>(.*?)\<endlink\>/', '<a href="\\1.php">\\1</a>', $testtxt);
echo "The test text is".$testtxt;

This works fine. However when I pull it from the db it doesn't work:

$qrytxt = "Select * from texts where text_id='2'";
$resulttxt = mysql_query($qrytxt);
$resultsettxt = mysql_fetch_array($resulttxt);
$txt = $resultsettxt['text'];
$newtxt = preg_replace('/\<link\>(.*?)\<endlink\>/', '<a href="\\1.php">\\1</a>', $txt);
echo $newtxt;

Any ideas why that wouldn't work?

03-11-2010, 03:15 PM
What is $txt at the time you echo $newtxt?

03-11-2010, 03:21 PM
Ok, I did a little troubleshooting and figured it out. The only other thing I would like to happen is regardless of what is selected in the middle. Like if it's "Thanks", I'd like it to go to "thanks" so always lowercase.

03-11-2010, 04:05 PM

Depending on your preference you might want to use an anonymous function for the callback: http://php.net/manual/en/functions.anonymous.php