Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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