help with regular expression would be welcomed
Moderator: General Moderators
help with regular expression would be welcomed
Hello
I am quite new to regular expression and I am faced with a challange that I feel could be solved quite easily using regular expression.
I would appreciate if someone could help me with the solution and if possible to explain how he/she solved it so I will pick up my regular expression knowledge.
Here is the problem:
I am given a PHP string like so:
$phrase = 'Jo?n* Mil??er'
Where * means zero or more occurances of any letter (except whitespace)
and ? means a single occurance of any letter (except whitespace)
I then get another sentence (in another PHP string) in which the phrase was found. For example: 'Johnny Miller was here'
I need to use regular expressions to highlight the phrase in the sentence (using the <b> tag).
So in the example above, the highlighted expression will be:
'<b>Johnny Miller</b> was here'
Here is another example:
$phrase = '*a*'
$sentence = 'The tall man walked down the street'
The highlighted sentecne will be:
'The <b>tall</b> <b>man</b> <b>walked</b> down the street''
or in HTML:
'The tall man walked down the street'
I would appreciate any help
regards
Jason
I am quite new to regular expression and I am faced with a challange that I feel could be solved quite easily using regular expression.
I would appreciate if someone could help me with the solution and if possible to explain how he/she solved it so I will pick up my regular expression knowledge.
Here is the problem:
I am given a PHP string like so:
$phrase = 'Jo?n* Mil??er'
Where * means zero or more occurances of any letter (except whitespace)
and ? means a single occurance of any letter (except whitespace)
I then get another sentence (in another PHP string) in which the phrase was found. For example: 'Johnny Miller was here'
I need to use regular expressions to highlight the phrase in the sentence (using the <b> tag).
So in the example above, the highlighted expression will be:
'<b>Johnny Miller</b> was here'
Here is another example:
$phrase = '*a*'
$sentence = 'The tall man walked down the street'
The highlighted sentecne will be:
'The <b>tall</b> <b>man</b> <b>walked</b> down the street''
or in HTML:
'The tall man walked down the street'
I would appreciate any help
regards
Jason
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
<?php
function phraseHighlight($phrase, $text)
{
$convertFrom = array('#(?<!\\\\\\\\)\\\\?#', '#(?<!\\\\\\\\)\\\\*#', '#@#');
$convertTo = array('[a-z]','[a-z]*?','\\\\@');
$phrase = preg_replace($convertFrom, $convertTo, $phrase);
$matches = array();
preg_match_all('@\\b' . $phrase . '\\b@i', $text, $matches);
$matches = array_unique($matches[0]);
foreach($matches as $match)
$text = preg_replace('@\\b' . preg_quote( $match, '@' ) . '\\b@', '<b>\\\\0</b>', $text);
return $text;
}
echo phraseHighlight( 'Jo?n* Mil??er', 'Johnny Millner was here.' )."\\n";
echo phraseHighlight( '*a*', 'The tall man walked down the street' )."\n";
echo phraseHighlight( '*a*', '<a href="blahblah kitty">test have ten base</a>');
?>Code: Select all
<b>Johnny Millner</b> was here.
The <b>tall</b> <b>man</b> <b>walked</b> down the street
<<b>a</b> href="<b>blahblah</b> kitty">test <b>have</b> ten <b>base</b></<b>a</b>>
Last edited by feyd on Fri Dec 31, 2004 10:26 am, edited 1 time in total.
I get a strange PHP error when I try your code:
where line 5 is the line:
what could be the problem?
Code: Select all
Warning: preg_replace() їfunction.preg-replace]: Compilation failed: missing ) at offset 8 in C:\www\test.php on line 5Code: Select all
$phrase = preg_replace($convertFrom, $convertTo, $phrase);- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
(?!...) (?=...) (?<!...) (?<=...) are forward (first 2) and back (last 2) references, or look-forwards and look-backs. They check the space around them for the contained pattern.
[] are metacharacters that denote a character class beginning and end, respectively. The a-z in them tells the pattern to match letters a through z.
* is a metacharacter that denotes a match against zero or more of the preceeding character or grouping.
? is a metacharacter that denotoes a match against zero or one of the preceeding character or grouping.
*? is a metacharacter combination that tells the match to be as small as possible.
[] are metacharacters that denote a character class beginning and end, respectively. The a-z in them tells the pattern to match letters a through z.
* is a metacharacter that denotes a match against zero or more of the preceeding character or grouping.
? is a metacharacter that denotoes a match against zero or one of the preceeding character or grouping.
*? is a metacharacter combination that tells the match to be as small as possible.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Code: Select all
$convertTo = array('їa-z]?','їa-z]*?','\\@');