Solved

search with regular expression to find out some tag like XML

Posted on 2006-06-11
8
213 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update database from a form 8 35
php email 2 26
Preventing users manipulating URL strings 10 33
How to loop bootstrap columns which contain database records 9 19
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 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

16 Experts available now in Live!

Get 1:1 Help Now