Hi again,

Ok heres my problem:

Lets say I have a string which contains:
"Hi my name is Tim";

And i have another string containing a pattern:
"HI MY NAME IS *";

I wanna match the pattern to the input and replace the star with what they put. How can I do this in PHP?

Recommended Answers

All 11 Replies

Please reply.

Sounds like you want a regular expression. A regular expression lets you do pretty sophisticated pattern matching. However, be warned that they can be slow.

<?php
$incomingString = 'Hi, my name is John Doe';
$rxIntroduction = '/HI,? MY NAME IS ([\w ]+)\.?/i';
$matches = array();

// preg_match() :  http://php.net/manual/en/function.preg-match.php
if(preg_match($rxIntroduction, $incomingString, $matches)) {
	print_r($matches);
	$name = $matches[1];
	// do something
}
?>

Mmmm, it doesn't work.

Edit: It only gives: Array()

Oh. I get this when I run it in the cli:

Array
(
    [0] => Hi, my name is John Doe
    [1] => John Doe
)

Hmm, is there another way?

Is the input the entire sentence, or just the name "Tim"? In the case of the latter, and aside from capitalisation, and assuming it's only the asterisk you ever want to replace, then use this:

$input = "Tim";
$pattern = "Hi my name is *";
$output = str_replace("*",$input,$pattern);

Or is "Tim" the thing you want to output?

I want to input the whole sentence, but using the asterisk as a wild card, I want to find out what is in the place of the asterisk using php.

Ok, here you go:

<?php

$p1 = "Hi my name is *";
$p2 = "Hi my name is Edwin";

preg_match(str_replace('*','(.*)','/^'.preg_replace_callback('/(.)/miU',create_function('$c','return ($c[1] !== \'*\') ? \'\\x\'.str_pad(dechex(ord($c[1])),2,"0",STR_PAD_LEFT) : \'*\';'),$p1).'$/miU'),$p2,$matches);
$output = $matches[1];

echo $output;	// Edwin

?>

I've tested the code and it appears to work as intended. So for example if you had:

$p1 = "I said that * was my name";
$p2 = "I said that Tim was my name";

Then the output is "Tim"

So this code grabs what the put in the place of the asterisk? If so, then Thank you very much.

But do you think you can make it a little less complicated.

Edit: Wow, works perfectly, thank you very much.

Actually I've just found a way of shortening it even further:

<?php

$p1 = "Hi my name is *";
$p2 = "Hi my name is Edwin";

preg_match(str_replace('*','(.*)','/^'.preg_replace_callback('/([^\*])/miU',create_function('$c','return \'\\x\'.str_pad(dechex(ord($c[1])),2,"0",STR_PAD_LEFT);'),$p1).'$/miU'),$p2,$matches);
$output = $matches[1];

echo $output;	// Edwin

?>

But if you want a more expanded version so that you can work out what each part is doing:

<?php

$p1 = "Hi my name is *";
$p2 = "Hi my name is Edwin";

function get_PCRE_hex_escape_sequence($matches) {
$ascii_decimal = ord($matches[1]);
$ascii_hex = dechex($ascii_decimal);
$ascii_hex_zero_padded = str_pad($ascii_hex,2,"0",STR_PAD_LEFT);
$escape_sequence = '\x'.$ascii_hex_zero_padded;
return $escape_sequence;
}

$escaped_pattern = preg_replace_callback('/([^\*])/miU','get_PCRE_hex_escape_sequence',$p1);
$escaped_regular_expression = '/^'.$escaped_pattern.'$/miU';
$completed_regular_expression = str_replace('*','(.*)',$escaped_regular_expression);

preg_match($completed_regular_expression,$p2,$matches);
$output = $matches[1];

echo $output;	// Edwin

?>

I noticed I forgot to answer your question earlier:

So this code grabs what the put in the place of the asterisk? If so, then Thank you very much.

Yes, that is exactly what this code does. Based on the pattern ($p1) and the string ($p2) it works out what the asterisk represents.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.