Solved

search with regular expression to find out some tag like XML

Posted on 2006-06-11
8
214 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to properly use session_regenerate_id when user logs in 7 37
tutorial for ebay api 3 32
maybe no no httpd.conf 6 46
Cookie not unsetting 7 18
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

776 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