05-09-08, 08:12 PM
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?
05-09-08, 08:14 PM
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> ?
05-09-08, 08:24 PM
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?
05-09-08, 08:29 PM
Aspiring Coder
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
__________________
Can you think outside the box but remain inside the box?
05-09-08, 09:08 PM
Code Master
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
Last edited by Jay6390; 05-09-08 at 09:13 PM .
05-09-08, 09:08 PM
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.
05-09-08, 09:12 PM
Newbie Coder
Join Date: Feb 2007
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Here is your source code, multiple similar elements:
This will pick up the first one only, even if there is non-word character such as new line, tab etc. :
05-09-08, 09:27 PM
Aspiring Coder
Join Date: Aug 2007
Posts: 540
Thanks: 0
Thanked 0 Times in 0 Posts
So if I havethis
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?
05-09-08, 10:05 PM
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.
05-09-08, 10:16 PM
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>" ;
?>
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off