Solved

search with regular expression to find out some tag like XML

Posted on 2006-06-11
8
215 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

 

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Installer 5 38
check mysql insert 12 28
PHP website on Linux - server DNS address could not be found. 18 48
Force PDF to open inline as opposed to dowload 16 23
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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