Page 1 of 1

Parsing strings on certain conditions and replacing with act

Posted: Sat Aug 31, 2002 10:05 am
by webpoet
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

Posted: Sat Aug 31, 2002 10:39 am
by volka
although I'm certain there is a more elegant way, this comes to my mind

Code: Select all

&lt;?php
function showlogo()
{
	return '--result of showlogo--';
}

$string = "Hello and welcome to my teststring! &#1111;showlogo] I hope you will enjoy your stay here. &#1111;anotheraction] the End";

$pattern_match = '!(?&lt;=\&#1111;)\w+(?=\])!'; // without &#1111; ] matching
$pattern_split = '!\&#1111;\w*\]!'; // with &#1111; ] matching

$matches = array();
if (preg_match_all($pattern_match, $string, $matches))
{
	$matches = $matches&#1111;0];
	$remaining = preg_split($pattern_split, $string);
	
	for($i = 0; $i!=count($matches); $i++)
	{
		print($remaining&#1111;$i]);
		if (function_exists($matches&#1111;$i]))
		{
			$cmd =	'print('.$matches&#1111;$i].'());';
			eval($cmd);
		}
		else
			print('&#1111;'.$matches&#1111;$i].']');
	}
	print($remaining&#1111;$i]);
}
else
	print($string);
?&gt;

Posted: Sat Aug 31, 2002 11:20 am
by Takuma
Maybe you could do this using str_replace, it's faster than Perl Regular Expression when dealing with really long string.

Code: Select all

&lt;?php
  function showlogo() {
    return "--RESULT--";
  }

  $showlogo = showlogo();

  $string = "Hello and welcome to my teststring! &#1111;showlogo] I hope you will enjoy your stay here. &#1111;anotheraction]";
  $string = str_replace("&#1111;showlogo]",$showlogo,$string);

  echo $string;
?&gt;
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.

Posted: Sat Aug 31, 2002 11:35 am
by hob_goblin
He said that they would contain different strings to mark them, in which case he would want to use regexps

Parsing strings on certain conditions and replacing with act

Posted: Sat Aug 31, 2002 11:38 am
by gite_ashish
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.

:idea: See the php manual for function details.

Code: Select all

&lt;?php

$string = "Hello and welcome to my teststring! &#1111;showlogo] I hope you will enjoy your stay here. &#1111;anotheraction]" 

$string = str_replace( "&#1111;showlogo]", "&lt;IMG src=/images/logo.png&gt;", $string );

$string = eregi_replace( "&#1111;AnoTHEraCtiON]", "&lt;HR size=1 noShade&gt;Thank you for vist !!", $string );

?&gt;
:!: The Find and Replace strings can be taken from the respective arrays also:

Code: Select all

&lt;?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 '&lt;B&gt;Befor replace:&lt;/B&gt;&lt;BR&gt;' . $strText . '&lt;HR&gt;';

$strMS = str_replace( $strFind, $strReplace1, $strText );
echo '&lt;B&gt;Find-n-Replace for MS :&lt;/B&gt;&lt;BR&gt;' . $strMS . '&lt;HR&gt;';

$strL = str_replace( $strFind, $strReplace2, $strText );
echo '&lt;B&gt;Find-n-Replace for Linux :&lt;/B&gt;&lt;BR&gt;' . $strL . '&lt;HR&gt;';

?&gt;
8) All the best,


Posted: Sat Aug 31, 2002 11:46 am
by webpoet
volka, thanx, this seems to be what I´m looking for. but now it only parses the first []. How do I make it so that it works with all occurances of [] ?

Posted: Sat Aug 31, 2002 11:51 am
by volka
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 ;)

Posted: Sat Aug 31, 2002 11:55 am
by webpoet
Thank you! Works perfect. You rock =)

Posted: Sat Aug 31, 2002 12:06 pm
by volka
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 ...

Posted: Sat Aug 31, 2002 12:51 pm
by webpoet
Well, I´m trying to build a little system where the admins can create plaintext or html pages on their own that´s stored in a DB and call a set of functions to enhance it with certain server side stuff, ie a guestbook or a forum