PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Thu Aug 22, 2019 3:12 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Fri Dec 31, 2004 8:48 am 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 10:39 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA


Last edited by feyd on Fri Dec 31, 2004 11:26 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 11:19 am 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 11:25 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 11:28 am 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 11:30 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 11:33 am 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 11:53 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
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.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 31, 2004 12:17 pm 
Offline
Forum Contributor

Joined: Tue Jul 27, 2004 6:19 am
Posts: 206
what if I wanted to modify the defintion of the '?' character to mean
zero or 1 occurances instead of single occurance as I initialy requested?
what change do I need to make?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 01, 2005 1:38 am 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
Syntax: [ Download ] [ Hide ]
$convertTo = array('&#1111;a-z]?','&#1111;a-z]*?','\\@');


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group