Parsing strings on certain conditions and replacing with act
Moderator: General Moderators
Parsing strings on certain conditions and replacing with act
I´m a total clueless newbie when it comes to parsing in PHP so any help is greatly appreciated.
What I would like to do is the following:
Say I have a string that looks like this:
$string = "Hello and welcome to my teststring! [showlogo] I hope you will enjoy your stay here. [anotheraction]"
This string could ofcourse look in many ways but all strings will contain one or several words in [ ] or someother char to mark them. I´m trying to make some code that will go thru this string and replace everything withinit with results from different types of functions, for instance [showlogo] would call the showlogo() function which would return an image code.
Then lastly I want to echo the string with the edits, or echo each part before or after the [ ] action with the actions results in between.
So for instance my example string would send the following to a visitors browser:
Hello and welcome to my teststring! <URL-TO-LOGO> I hope you will enjoy your stay here. <RESULT-FROM-ANOTHER-ACTION>
Any ideas on how to do this ?
Thanks in advance,
Joel
What I would like to do is the following:
Say I have a string that looks like this:
$string = "Hello and welcome to my teststring! [showlogo] I hope you will enjoy your stay here. [anotheraction]"
This string could ofcourse look in many ways but all strings will contain one or several words in [ ] or someother char to mark them. I´m trying to make some code that will go thru this string and replace everything withinit with results from different types of functions, for instance [showlogo] would call the showlogo() function which would return an image code.
Then lastly I want to echo the string with the edits, or echo each part before or after the [ ] action with the actions results in between.
So for instance my example string would send the following to a visitors browser:
Hello and welcome to my teststring! <URL-TO-LOGO> I hope you will enjoy your stay here. <RESULT-FROM-ANOTHER-ACTION>
Any ideas on how to do this ?
Thanks in advance,
Joel
although I'm certain there is a more elegant way, this comes to my mind
Code: Select all
<?php
function showlogo()
{
return '--result of showlogo--';
}
$string = "Hello and welcome to my teststring! їshowlogo] I hope you will enjoy your stay here. їanotheraction] the End";
$pattern_match = '!(?<=\ї)\w+(?=\])!'; // without ї ] matching
$pattern_split = '!\ї\w*\]!'; // with ї ] matching
$matches = array();
if (preg_match_all($pattern_match, $string, $matches))
{
$matches = $matchesї0];
$remaining = preg_split($pattern_split, $string);
for($i = 0; $i!=count($matches); $i++)
{
print($remainingї$i]);
if (function_exists($matchesї$i]))
{
$cmd = 'print('.$matchesї$i].'());';
eval($cmd);
}
else
print('ї'.$matchesї$i].']');
}
print($remainingї$i]);
}
else
print($string);
?>Maybe you could do this using str_replace, it's faster than Perl Regular Expression when dealing with really long string.
This might be not a good idea if you've got a lot of strings to replace but if that's the case I think you can make a function which replaces the string.
Code: Select all
<?php
function showlogo() {
return "--RESULT--";
}
$showlogo = showlogo();
$string = "Hello and welcome to my teststring! їshowlogo] I hope you will enjoy your stay here. їanotheraction]";
$string = str_replace("їshowlogo]",$showlogo,$string);
echo $string;
?>- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
- gite_ashish
- Forum Contributor
- Posts: 118
- Joined: Sat Aug 31, 2002 11:38 am
- Location: India
Parsing strings on certain conditions and replacing with act
Hi,
1. You can use str_replace() for non-regex find-n-replace.
2. You can use ereg_replace() for regex find-n-replace. The eregi_replace() is case insensetive find-n-replace.
See the php manual for function details.
The Find and Replace strings can be taken from the respective arrays also:
All the best,
1. You can use str_replace() for non-regex find-n-replace.
2. You can use ereg_replace() for regex find-n-replace. The eregi_replace() is case insensetive find-n-replace.
Code: Select all
<?php
$string = "Hello and welcome to my teststring! їshowlogo] I hope you will enjoy your stay here. їanotheraction]"
$string = str_replace( "їshowlogo]", "<IMG src=/images/logo.png>", $string );
$string = eregi_replace( "їAnoTHEraCtiON]", "<HR size=1 noShade>Thank you for vist !!", $string );
?>Code: Select all
<?php
$strFind = array( '{vendor}', '{os}', '{office}' );
$strReplace1 = array( 'Micrsoft', 'Windows 2000', 'Office XP' );
$strReplace2 = array( 'Red Hat', 'Linux 7.3', 'Star Office' );
$strText = 'This is the sample text. You can use {vendor} product for your hardware. The lattest version is {os}. For your office users, do use {office}. Thank you !';
echo '<B>Befor replace:</B><BR>' . $strText . '<HR>';
$strMS = str_replace( $strFind, $strReplace1, $strText );
echo '<B>Find-n-Replace for MS :</B><BR>' . $strMS . '<HR>';
$strL = str_replace( $strFind, $strReplace2, $strText );
echo '<B>Find-n-Replace for Linux :</B><BR>' . $strL . '<HR>';
?>hmmm...the code does this.
in the example the second replacement is [anotheraction].
The script takes all "words" between [ and ] and tries to find a corresponding function ( function_exists ). If it exists it's called via eval(), if not the orginal 'tag' is displayed.
anotheraction() is not defined in the example
in the example the second replacement is [anotheraction].
The script takes all "words" between [ and ] and tries to find a corresponding function ( function_exists ). If it exists it's called via eval(), if not the orginal 'tag' is displayed.
anotheraction() is not defined in the example
But consider the other posts, too.
If your replacements are constant strings str_replace is the faster, smaller, better solution, as mentioned above.
Are you sure it's worth using preg_ and eval()? It only is, if you're creating 'dynamic' contents like (i.e) grabbing the first word of another webpage or counting something or ...
If your replacements are constant strings str_replace is the faster, smaller, better solution, as mentioned above.
Are you sure it's worth using preg_ and eval()? It only is, if you're creating 'dynamic' contents like (i.e) grabbing the first word of another webpage or counting something or ...