PHP String operations

Hi I need a PHP function that will located these tags {} and replace them with some content.

http://www.google.com?myvar1={id1}&myvar2={id2}&myvar1={id3}&myvar2={id4}

For instance myvar2={id2} must be replaced with myvar2=$my_row('id2') etc...

So it must locate {} and grab whatever is in between (i.e. id2) and use it with mysql.

Here's what the final result must look like:

http://www.google.com?myvar1=123&myvar2=456&myvar1=789&myvar2=474

thanks
GagikAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

subscriptCommented:
What about using str_replace function:

Example:

// Provides: You should eat pizza, beer, and ice cream every day
$phrase  = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$newphrase = str_replace($healthy, $yummy, $phrase);

From:
http://us2.php.net/manual/en/function.str-replace.php

Does that work for you?
0
GagikAuthor Commented:
it will not work right away because I never know what asre these tags.
i first must make a search in order to make alist of all existing tags, i.e. {id1}, {id2}, {id3}... and then I will be able to replace it.
id1, id2, id3 ... they all correspond to a field in MySQL table. after having located these tags I will replace these parts of the string with $row['id1'], $row['id2'], $row['id3']...

So the first step is to find a way of making a list of these tags. I must refer to these brackets {}
0
subscriptCommented:
Here's a sample of how to extract the pattern and values from a string.

<?php
$sample_html = "<pre><b>bold text {12} </b><a href=howdy.html>click me here {18}</a></pre>";
echo 'Sample_html_string = ' . $sample_html . '<br />';

echo 'Goal: Find all curly braces and values between them.<br />';

/* match all curly brace patterns, and the values between them */
preg_match_all("/{(.*?)}/", $sample_html, $matches);

echo '<br />Done!<br /><br />Pattern match dump....<br />';

/* See matched pattern */
print_r($matches[0]);

echo '<br /><br />Values within the braces dump...<br />';

/* See matched pattern value */
print_r($matches[1]);
?>

Does that help at all?

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GagikAuthor Commented:
I created my own 2 functions. I'm going to try your code too though because it looks simpler. Here's mine:


	private function getQueryStringTags($str){
		
		$qsTags = array();
		
		$beg=0;
		$end=0;
	
		//print '<br />'.$str.'<br />';
		
		while( $beg < strlen($str) ){
			
			$beg = strpos($str, '{', $beg);		
			
			$end = strpos($str, '}', $end+1);		
			
			array_push($qsTags, substr($str, $beg+1, $end-$beg-1) );
			
			$beg = $end+1;
			
		}
		
		//print_r($qsTags).'<br />';
		
		return $qsTags;		
	}
	
	private function replaceTags( $str, $row, $qsTags ){
		
		foreach( $qsTags as $key => $val ){
			
			$str = str_replace('{'.$val.'}', $row[$val], $str);			
		
		}
		
		return $str;		
	}

Open in new window

0
subscriptCommented:
Any luck?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.