• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 221
  • Last Modified:

search with regular expression to find out some tag like XML

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
tunhien
Asked:
tunhien
  • 4
  • 4
1 Solution
 
RoonaanCommented:
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
 
tunhienAuthor Commented:
Thanks!
That good!

Is any good site help me to learn about Regular Expression!?
Thanks angain!
0
 
RoonaanCommented:
The website www.regular-expression.info provides lots of information on regular expressions, including some tutorials and example codes.

-r-
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
tunhienAuthor Commented:
And If I wanna case insensitive with the "IMG" string in those tags.
Please show me what the expression would be!?
0
 
RoonaanCommented:
The regular expression should be case insensitive.

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

-r-
0
 
tunhienAuthor Commented:
wow! That's greate!

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

Thanks
0
 
RoonaanCommented:
You should change "#i" to "#" (removing the "i")

-r-
0
 
tunhienAuthor Commented:
Oh!
Thank you very much! ;)
0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now