Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-02-2009, 08:30 AM   PM User | #1
snowysweb
Regular Coder

 
Join Date: Sep 2008
Posts: 280
Thanks: 25
Thanked 9 Times in 9 Posts
snowysweb is an unknown quantity at this point
regular expression

Ok, i've been trying to learn regular expressions.

Code:
([\s|.]*<(p|P|(H|h)(?(3)[1-6]))+>(.*)</(p|P|(H|h)(?(3)[1-6)]))+>+[\s|.]*)
is the first i have wrote from scratch, it finds header tgs and paragraph tags (with no atributes)

Code:
<h3>Commercial & Domestic</h3>
it would pick up.

Problem is

Code:
<h3>Commercial & 
Domestic</h3>
or

Code:
<h3>
Commercial & Domestic
</h3>
It would not pick up

anyone know of a way to help?
snowysweb is offline   Reply With Quote
Old 05-02-2009, 09:23 AM   PM User | #2
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
Your regexp seems very ineffecient.. I made this up, and the flag "s" treats the string as a single line.

PHP Code:
<?php

$string 
= <<<HTML
<h2>Header</h2>
<p>Paragraph</p>
HTML;

preg_match_all('#<(?:h[1-6]|p)>(.*?)</(?:h[1-6]|p)>#is'$string$matches);

print_r($matches);
Iszak is offline   Reply With Quote
Old 05-02-2009, 09:47 AM   PM User | #3
snowysweb
Regular Coder

 
Join Date: Sep 2008
Posts: 280
Thanks: 25
Thanked 9 Times in 9 Posts
snowysweb is an unknown quantity at this point
Right, could you explain to me is PHP regular expression different to other regular expression? for instance, i've downloaded a program called Expresso code project to test regular expressions. I copyied and paste what you put into there and tested and came up with no results. (apart from when i took away (# and #is, but then i was where i was before)
snowysweb is offline   Reply With Quote
Old 05-02-2009, 09:53 AM   PM User | #4
snowysweb
Regular Coder

 
Join Date: Sep 2008
Posts: 280
Thanks: 25
Thanked 9 Times in 9 Posts
snowysweb is an unknown quantity at this point
Also, you used ?: when i want to match that as i want to know what data came from what tag.
snowysweb is offline   Reply With Quote
Old 05-02-2009, 10:53 AM   PM User | #5
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
Okay well if you want to match what tag it is simply remove the ?: like you said on the first one. Also I've never used Expresso Code Project, and I can't even manage to get your regular expression to work in PHP by simply adding decimeters like // (or ##, @@, etc). So it's possible that your Expresso Code Project tool is making incompatible regular expressions or you are. Develop them in PHP and test them in PHP. Unless you're using ereg? Seriously I would just scrap your last one and stick with the one I made although it's not perfect it's not as messy and inefficient as yours. Personally I don't recommend using a program to make regexp so if you are, I'd reconsider.

Last edited by Iszak; 05-02-2009 at 10:57 AM..
Iszak is offline   Reply With Quote
Old 05-02-2009, 11:32 AM   PM User | #6
snowysweb
Regular Coder

 
Join Date: Sep 2008
Posts: 280
Thanks: 25
Thanked 9 Times in 9 Posts
snowysweb is an unknown quantity at this point
what i cant seem to get to work is the () (?($1)do this|or this)


Is this right or have i ot it round my neck?

if submatch $1 is set then do this.

thats what i want.
snowysweb is offline   Reply With Quote
Old 05-02-2009, 08:17 PM   PM User | #7
Iszak
Regular Coder

 
Iszak's Avatar
 
Join Date: Jun 2007
Location: Perth, Western Australia
Posts: 332
Thanks: 2
Thanked 58 Times in 57 Posts
Iszak is an unknown quantity at this point
No that is wrong, if you want to use something you matched prior in the regular expression you use \n for single quotes or \\n for double quotes (where n is a number) e.g.
Code:
preg_match_all('#<(h[1-6]|p)>(.*?)</\1>#is', $string, $matches);
So whatever they matched first must be used later and you can't do <h1>Header</p>
Iszak is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:07 PM.


Advertisement
Log in to turn off these ads.