Current location: Hot Scripts Forums » Programming Languages » PHP » Preg_match()


Preg_match()

Reply
  #1 (permalink)  
Old 05-09-08, 08:12 PM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
Preg_match()

Hey,

How can I match this:

<h1><font size="2">Any character - + = ' @ / ? > < : ; # @~ & * ( ) ^ %$£ "!¬`, . </font></h1>

I want to match that but put that in array[0] and then the rest of the content in array[1] but it can only happen at the start of the string variable.

Could someone help me with the pattern using preg_match() please?
__________________
Can you think outside the box but remain inside the box?
Reply With Quote
  #2 (permalink)  
Old 05-09-08, 08:14 PM
BurakUeda BurakUeda is offline
Newbie Coder
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
You want to match this part only?
Any character - + = ' @ / ? > < : ; # @~ & * ( ) ^ %$£ "!¬`, .

or the whole line from <h1> to </h1> ?
Reply With Quote
  #3 (permalink)  
Old 05-09-08, 08:24 PM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
The whole line from <h1> any character ; : ' @ etc.. </h1> only once.It will always be at the beginning.
__________________
Can you think outside the box but remain inside the box?
Reply With Quote
  #4 (permalink)  
Old 05-09-08, 08:29 PM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:


(<h1><font size="2">)(0-9a-zA-Z-@&:;,)(</font></h1>) 
?
__________________
Can you think outside the box but remain inside the box?
Reply With Quote
  #5 (permalink)  
Old 05-09-08, 09:08 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Code:
(<h1><font size="2">.*?<\/font><\/h1>)
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName

Last edited by Jay6390; 05-09-08 at 09:13 PM.
Reply With Quote
  #6 (permalink)  
Old 05-09-08, 09:08 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Try:

(<h1><font size="2">(.+)</font></h1>)

Jay's solution works too.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #7 (permalink)  
Old 05-09-08, 09:12 PM
BurakUeda BurakUeda is offline
Newbie Coder
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Here is your source code, multiple similar elements:
PHP Code:

<?php

$a 
= <<<HTML
<h1><font size="2">Any character First - + = ' @ / ? > < : 
; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>

<h1><font size="2">Any character Second- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>

<h1><font size="2">Any ch
aracter Third- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>

<h1><font size="2">Any character Fourth- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>

<h1><font size="2">Any character Fifth- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>

<h1><font size="2">Any character Sixth- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>
HTML;
?>
This will pick up the first one only, even if there is non-word character such as new line, tab etc. :
PHP Code:

preg_match("/(\<h1\>)(.|\W)+(\<\/h1\>)/U",$a,$match);


echo 
"<pre>".htmlspecialchars($match[0])."</pre>"
Reply With Quote
  #8 (permalink)  
Old 05-09-08, 09:27 PM
blinn_shade's Avatar
blinn_shade blinn_shade is offline
Aspiring Coder
 
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
So if I havethis

PHP Code:


'<h1><font size="2">Some product name</font></h1>fghfghfghfghfghfghfgh' 
How can I get the rest of the string in an array such as: fghfghfghfghfghfghfgh
__________________
Can you think outside the box but remain inside the box?
Reply With Quote
  #9 (permalink)  
Old 05-09-08, 10:05 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
I highly recommend downloading something called The Regex Coach, which allows you to work interactively with regular expressions. It's free and it's saved me a ton of time and head-scratching when it comes to figuring out regular expression patterns.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #10 (permalink)  
Old 05-09-08, 10:16 PM
BurakUeda BurakUeda is offline
Newbie Coder
 
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php

$a 
= <<<HTML
<h1><font size="2">Any character First - + = ' @ / ? > < : 
; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>fghf 1234 ghf.ghfghfghfghfgh

<h1><font size="2">Any character Second- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>asasasas 9879 asasas

<h1><font size="2">Any ch
aracter Third- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>sdsdsdsdsdsdsds

<h1><font size="2">Any character Fourth- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>ghghghgh - hlkjhk 99097 ghghghg

<h1><font size="2">Any character Fifth- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>yuyuyuyuyuyuyu

<h1><font size="2">Any character Sixth- + = ' @ / ? > < : ; # @~ & * ( ) ^ %
 "!¬`, . </font></h1>opopopopopopopo
HTML;

preg_match("/(\<h1\>)(.|\W)+(\<\/h1\>)/iU",$a,$match); // Whole Line
preg_match("/(?<=\<\/h1\>)[\w \/\.\^-]+/i",$a,$match2); // Rest of the string

echo "<pre>".htmlspecialchars($match[0])."</pre>";
echo 
"<pre>".htmlspecialchars($match2[0])."</pre>";
?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT -5. The time now is 07:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.