preg_match - How do I...?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

preg_match - How do I...?

Post by sweahe »

Say I want to find in a string the word 'car' and after that it can be anything any lenght but the word 'hotel' and after that it can be anything any lenght and then the word 'hello' should come.

How would that perl regex look like?? How do I write to not allowing a word like 'hotel' inside this string?

/Andreas
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

You should read up on preg_match(): preg_match ( string pattern, string subject [, array matches]).

Code: Select all

<?php
$string = "the car is parked in the hotel";
if( preg_match("hotel", $string) )
&#123;
   echo"hello";
&#125;
?>
Hope thats what you wanted :D
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Nope!
Say I want car --- NOT: parked --- hotel
Try this:

Code: Select all

<?php 
$string = "the car is parked in the hotel, no the car run into the hotel!"; 
...???
?>
What I want it to match here is:
car run into the hotel
not:
car is parked in the hotel
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

Can you be more descriptive in what string(exactly) you want searched, what word your looking for, and how to handle the word when it is found.
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Exactly what I want to find is this:

Code: Select all

$text = 'This is a test <a href="http://www.company.com">test</a>some text<a href="#">muppo</a>whackoo!!';
I want it to match first '<a ' then following anything but '</a>' and then anything until 'muppo'

So it would find:
<a href="#">muppo

And not:
<a href="http://www.company.com">test</a>some text<a href="#">muppo

Is it clear?

Thanks!
Andreas
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

How can I do a not this word thing??
I know I can do a not any of these chars like:
[^fghav]
..but how can I prevent a full word like: '</a>' ?
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

Code: Select all

$text = 'This is a test <a href="http://www.company.com">test</a>some text<a href="#">muppo</a>whackoo!!'; 

if( preg_match("<a href="#"></a>$", $text)
&#123;
echo"true";
&#125;
???
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Que? I'm sorry I don't think you understand what I want to do here...
User avatar
fatal
Forum Contributor
Posts: 118
Joined: Sat Apr 20, 2002 10:47 am
Location: East Coast

Post by fatal »

To be honest, i dont think you know what you want.
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Sure I do! :)
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Post by sweahe »

Almost there now:
$search_pattern = '/<[aA] [^<][^/][^aA]*muppo/';

This finds:
<a href="#">muppo

But why can't I put in the last character too the > in </a>
Like this:
$search_pattern = '/<[aA] [^<][^/][^aA][^>]*muppo/';

This does not work!?? hmm....
Any ideas?
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

Code: Select all

<?
$text = "This is a test <a href="http://www.company.com">test</a> some text <a href="#">muppo</a> whackoo!!";
if(preg_match("/<a href=(.*)</a>/i",$text,$match)) &#123;
  echo $match&#1111;0];
&#125;
?>
in your next post, give an example of what you expect the results to be.
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

pftt....use the ereg* functions ;) </just to confuse things more>
sweahe
Forum Commoner
Posts: 52
Joined: Sat May 04, 2002 4:07 am
Location: Växjö, Sweden

Isn't ereg slower?

Post by sweahe »

I've heard ereg is slower than preg!?
Is it easier done with ereg?
dusty
Forum Contributor
Posts: 122
Joined: Sun Apr 28, 2002 9:52 pm
Location: Portsmouth, VA

Post by dusty »

when it comes to regex PERL > all :)
Post Reply