[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP syntax

Posted on 2011-05-04
12
Medium Priority
?
310 Views
Last Modified: 2012-08-14
I'm trying to write a script that find a certain link im continue somebody else work and don't really understand what he meant in the following


$pattern = "/<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/i"
0
Comment
Question by:Nura111
  • 5
  • 4
  • 3
12 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35691433
Looks like a REGEX pattern.  Please tell us what you're trying to find and give us an example of the inputs and the outputs you want.  I am sure we can help. ~Ray
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35691480
0
 

Author Comment

by:Nura111
ID: 35691506
yes and im trying to understand what this regular expression mean. right now I need to look at the results from the script that somebody else wrote so i need to understand what does he meant by this regular expression.
if you can tell me how to begin to read it,
Thank you!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 35691536
There are whole books written about regular expressions, and it would probably take a semester of an engineering curriculum to understand it all.  REGEX is an amazing thing - a language written almost entirely in punctuation.  Somewhat the opposite of "clear."  To jump-start your learning, read the wikipedia article, then make a Google search for "regular expression" and read some of the examples and guides you find there.  Best of luck, ~Ray

http://en.wikipedia.org/wiki/Regular_expression
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35691591
Afterthought... You will find examples of regular expressions like that one all over the WWW, and many of them are wrong in one way or another.  When you start writing your own regular expressions, do it as shown in this code snippet, with separate lines for separate parts and with comments to document the intent of each part of the REGEX.  This is a REGEX for validating an email address, something that does not have to be done via REGEX any more, now that we have the PHP function filter_var().
http://php.net/manual/en/function.filter-var.php
$regex
= '/'                       // START REGEX DELIMITER
. '^'                       // START STRING
. '[A-Z0-9_-]'              // AN EMAIL - SOME CHARACTER(S)
. '[A-Z0-9._-]*'            // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
. '@'                       // A SINGLE AT-SIGN
. '([A-Z0-9][A-Z0-9-]*\.)+' // A DOMAIN NAME PERMITS DOT, ENDS DOT
. '[A-Z\.]'                 // A TOP-LEVEL DOMAIN PERMITS DOT
. '{2,6}'                   // TLD LENGTH >= 2 AND =< 6
. '$'                       // ENDOF STRING
. '/'                       // ENDOF REGEX DELIMITER
. 'i'                       // CASE INSENSITIVE
;
if (!preg_match($regex, $email)) return FALSE;

Open in new window

0
 

Author Comment

by:Nura111
ID: 35691611
ok thank you ray, there is anyway that you can five me a regex for finding URL?
0
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 1336 total points
ID: 35691758
0
 

Author Comment

by:Nura111
ID: 35691809
Ok i just want to make sure i understand it right does $urlregex is pattern for the regular url as in http:www.domain.com ?  and does it also allows to find URL as name1-name2 pattern? (with -)

Thank you!
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35691834
Yes, in the example you have seen, $urlregex is the REGEX pattern.

If you are beginner to PHP, I don't recommend you to start with REGEX, since it's more complicated than the other methods in PHP.
0
 

Author Comment

by:Nura111
ID: 35691846
so what is my other options to find a URL in a content not with REGEX?
0
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 1336 total points
ID: 35691866
Use the following code:
$string = "This is the string texts from: http://test.com/test.php";
$strArray = explode(' ', $string);
foreach($strArray as $word)
{
	if (substr($word, 0, 7) == "http://" || substr($word, 0, 8) == "https://")
	{
		$url = $word;
		break;
	}
}

echo "Your URL: $url<br />";

Open in new window

0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …
Suggested Courses

834 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