Given the following:
$foo = "Yo [user Cobb] I heard you like dreams so I put a dream in yo dream in yo dream so you can dream while you dream while you dream."
I'd like to do this:
$foo = bar($foo);
echo $foo;
And get something like this:
Yo Cobb I heard you like dreams so I put a dream in yo dream in yo dream so you can dream while you dream while you dream.
I'm unsure of how the bar
function should work. I think this is doable with regular expressions but I personally find those hard to understand. Using the strpos function is another method but I wonder if there is a better solution.
Pseudocode is fine but actual code will be appreciated.
Edit:
These tags are not placeholders as the 2nd part is a variable value.
Edit:
All of the str_replace answers are incorrect as the tags contain variable content.
You could use preg_match_all to search the string for tags.
function bar($foo)
{
$count = preg_match_all("/\[(\w+?)\s(\w+?)\]/", $foo, $matches);
if($count > 0)
{
for($i = 0; $i < $count; $i++)
{
// $matches[0][$i] contains the entire matched string
// $matches[1][$i] contains the first portion (ex: user)
// $matches[2][$i] contains the second portion (ex: Cobb)
switch($matches[1][$i])
{
case 'user':
$replacement = tag_user($matches[2][$i]);
str_replace($matches[0][$i], $replacement, $foo);
break;
}
}
}
}
Now you can add more functionality by adding more cases to the switch.
As the tags contain content you want to parse and are not static to be replaced tags you’ll have to use regular expressions. (It’s the easiest way to go.)
preg_replace() is the regular expression function to replace text.
$pattern = '/\[user (\w+)\]/i';
$rpl = '<a href="http://example.com/user/${1}">${1}</a>';
return preg_replace($pattern, $rpl, $foo);
This will match for a [user xy] tag where xy is a word (sequence of word-characters) of at least one character. As it is in parenthesis it is accessible with {1} in the replace-string. $foo is the string you want to parse. Returned is the parsed string with replaced tag. The i
modifier on the pattern will make the matching case-insensitive. Remove it if you want it to be case-sensitive.
(The example you gave parses from [user Cobb] to a wikipedia url leonardo dicabrio, which is in no correspondence to neither user
nor Cobb
. So however you got there, you’ll have to do that (query a db? whatever). If it was just not careful enough providing example code; you probably wanted to point to a static url and add part of the tag content to it, which is what I did here.)
str_replace() is going to be your best option:
function bar($foo) {
$user = 'Cobb';
return str_replace('[user]', $user, $foo);
}
$foo = 'Yo [user] I heard you like dreams so I put a dream in yo dream in yo dream so you can dream while you dream while you dream.'
$foo = bar($foo);
print $foo; // Will print "Yo Cobb I heard you like dreams so I put a dream in yo dream in yo dream so you can dream while you dream while you dream."
What about str_replace?
function bar(foo) return str_replace($arrayWithStringsToGetReplaced, $arrayWithStringsToReplaceWith, $foo)
If I understand the comments below correctly.
This is obviously beyond me. Moving on... :)
Regular Expressions
are the way to go. Hard yes, but the benefit gained from learning them far outweighs the effort needed to learn.
From php.net
<?php
$text = 'The price is PRICE ';
$lookFor = 'PRICE';
$replacement = '$100';
echo $replacement.'<br />';
//will display
//$100
echo str_replace($lookFor, $replacement, $text).'<br />';
//Will display
//The price is $100
?>
精彩评论