Solved

search with regular expression to find out some tag like XML

Posted on 2006-06-11
8
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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
 
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 look for a specific file type in a local or remote server directory using PHP.

631 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