Solved

search with regular expression to find out some tag like XML

Posted on 2006-06-11
8
212 Views
Last Modified: 2008-03-06
If I've got a string like this:

===========================================
$str='
This is toan k1 image

[IMG]www.toank1.net/toank1.jpg[/IMG]

orther way is:
[IMG src="www.toank1.net/toank1.jpg"/]

and may be use this for default picture:
[IMG/]
';
===========================================

So I wanna get the array with resutl:
-------------------------
$arrResults = array('[IMG]www.toank1.net/toank1.jpg[/IMG]',
                             '[IMG src="www.toank1.net/toank1.jpg"/]',
                             '[IMG/]');
-------------------------
with the regular expression and the function in php is: preg_match_all

Please so me the expression string to do that!
Thanks alot!
0
Comment
Question by:tunhien
  • 4
  • 4
8 Comments
 
LVL 49

Accepted Solution

by:
Roonaan earned 125 total points
ID: 16880297
Here you go:

<?php

$str='
This is toan k1 image

[IMG]www.toank1.net/toank1.jpg[/IMG]

orther way is:
[IMG src="www.toank1.net/toank1.jpg"/]

and may be use this for default picture:
[IMG/]
';


  if(preg_match_all('#(\[IMG\](.*?)\[/IMG\])|(\[IMG[^\]]*?/\])#i', $str, $matches)) {
    var_export($matches[0]);
  }
?>

-r-
0
 

Author Comment

by:tunhien
ID: 16880301
Thanks!
That good!

Is any good site help me to learn about Regular Expression!?
Thanks angain!
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16880308
The website www.regular-expression.info provides lots of information on regular expressions, including some tutorials and example codes.

-r-
0
 

Author Comment

by:tunhien
ID: 16880310
And If I wanna case insensitive with the "IMG" string in those tags.
Please show me what the expression would be!?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 49

Expert Comment

by:Roonaan
ID: 16880315
The regular expression should be case insensitive.

The i at the end of the expression (#i) stands for case insensitivity.

-r-
0
 

Author Comment

by:tunhien
ID: 16880322
wow! That's greate!

so If I wanna case sensitive tag.
What should I replace?

Thanks
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16880353
You should change "#i" to "#" (removing the "i")

-r-
0
 

Author Comment

by:tunhien
ID: 16880363
Oh!
Thank you very much! ;)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now