The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages - More
> Regex Programming
|
How to make [^.....] for sting instead for chars...
Discuss How to make [^.....] for sting instead for chars... in the Regex Programming forum on Dev Shed. How to make [^.....] for sting instead for chars... Regular expressions forum covering PCRE and POSIX techniques, practices, and standards. Regular expressions help shorten coding time by providing the ability to compact many lines of code into one string.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 13th, 2009, 11:13 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 3
Time spent in forums: 1 h 38 m 23 sec
Reputation Power: 0
|
|
|
How to make [^.....] for sting instead for chars...
How to make my regexp to catch the following strings:
Quote: <A class="classname" href="http://domain/subfolders">some text <BR /> more text</A>
<A href="http://domain/subfolders" class=classname>some text <BR /> more text</A>
<A href="http://domain/subfolders" class="classname">some text more text</A> |
but not to catch the following:
Quote: | <A class="classname" href="http://domain/subfolders">some text more text</A>more text</A> |
to catch the first 3 strings i tried this:
Quote: | '@<a[^>]*classname[^>]*>.+</a>@i' |
Unfortunately it catches the second one too. I cannot figure it how to replace .+ so <br /> (or any other tag) to pass except </a> tag.
|

November 13th, 2009, 11:28 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Aren't you really just checking that there aren't two </a>s in the string?
|

November 13th, 2009, 11:51 PM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 3
Time spent in forums: 1 h 38 m 23 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by requinix Aren't you really just checking that there aren't two </a>s in the string? |
Here is an example what i'm trying to do:
PHP Code:
//I have a string i need to parse but for this example i'll create one from the 4 strings in my first post:
$string = '<A class="classname" href="http://domain/subfolders">some text <BR /> more text</A>';
$string .= '<A href="http://domain/subfolders" class=classname>some text <BR /> more text</A>';
$string .= '<A href="http://domain/subfolders" class="classname">some text more text</A>';
$string .= '<A class="classname" href="http://domain/subfolders">some text more text</A>more text</A>';
$array_categories=preg_match_all('@<a[^>]*classname[^>]*>.+</a>@i', $string, $matches);
print_r($matches);
i need to get output like this:
Quote: Array
(
[0] => Array
(
[0] => <A class="classname" href="http://domain/subfolders">some text <BR /> more text</A>
[1] => <A href="http://domain/subfolders" class=classname>some text <BR /> more text</A>
[2] => <A href="http://domain/subfolders" class="classname">some text more text</A>
)
) |
|

November 14th, 2009, 12:18 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Oh, that's all?
PHP Code:
preg_match_all('#<a[^>]*>.*?</a>#i', $string, $matches)
If you want the class=classname,
PHP Code:
preg_match_all('#<a[^>]*?class="classname"[^>]*>.*?</a>#', $string, $matches)
Much more and it'll be easier to do this with something like DOMDocument.
|

November 14th, 2009, 12:42 AM
|
|
Registered User
|
|
Join Date: Jun 2009
Posts: 3
Time spent in forums: 1 h 38 m 23 sec
Reputation Power: 0
|
|
wow it worked
i cannot still get the logic but that ? fixed it exactly as i need it.
Thank you 
|

November 14th, 2009, 12:57 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
The first one looks for a <a, grabs everything up to the next >, then finds everything up to the first </a> it encounters.
The second one looks for a <a, grabs some stuff, finds a class="classname", grabs everything else up to the next >, then finds everything up to the first </a> it encounters.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
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
|
|
|
|
|