Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Preg Match

This is a discussion on Preg Match, within the PHP section. This forum and the thread "Preg Match" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Preg Match
Jason
post Feb 15 2008, 11:50 AM
Post #1


Master of the Universe
Group Icon

Posts: 1,298
Joined: 15-February 08
From: London, England


Can someone explain to me how filters work? I have looked at the documentation and have swiftly got lost. I understand the basic syntax. Filters though...

Help on the following 2 examples would be incredibly helpful.

______________

[ b ]This is bold [/ b]

Without the spaces obviously. Now if I wanted to find the text between the bold tags how would I do this? It would be very helpful if someone could explain what filters are used and why.

______________


URL's. Lets say I want to find a web address and turn it into a hyperlink. How would I find

http://www.google.com/
http://www.google.co.uk/

Now I know that I am looking for something like this.

http://www. [ANYTHING] . [something]
http://www. [ANYTHING] . [something] . [something]

What filters do I need to use here?


If there are any other filters which are good to know or useful. Maybe some basic tips. Everything is welcome at this stage.


--------------------
Go to the top of the page
 
+Quote Post
joey
post Feb 15 2008, 12:15 PM
Post #2


Fresh Squeezed
**

Posts: 48
Joined: 14-February 08
From: Hillsboro, OR


I've used this website before a few times: http://www.regular-expressions.info/reference.html

For the website thing something like this should work for converting to a link:
CODE
if ($website <> "") {
    if (preg_match("/http/i", "$website")) {$wlink = $website;}
    else {$wlink = "http://$website";}
}

^^ I didn't test that but it should work. Sorry if it doesn't.


--------------------
Go to the top of the page
 
+Quote Post
rewake
post Feb 15 2008, 12:32 PM
Post #3


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


Hi Jason,

Here are some preliminary examples for you, off the top of my head. I'm sure you'll need to tweak things along the way.

1. The bold tags

What you're looking for is anything between and so your regexp might look like this...

CODE
$pattern = '/\[b\]([\w\s\d]+)\[\/b\]/';
$string = 'This is some bold text';

// preg_match() will tell you if there's a match in the string.
echo preg_match($pattern, $string);

// Now you can do this...
echo preg_replace($pattern, '$1',$string);


I'm sure you're going to need to alter the pattern to allow more chars though.

The URL one is a bit trickier since there are many variations you can use to write a URL... (http://site.com, http://www.site.com, site.com, sub.site.com, etc). The best way to go about this would be to validate the url text before-hand is possible. I'll get back to you when I have some time to work on it! I'm sure they already have some code out there for this as you see it on sites all the time.

Here's a link to one of my favourites tools...

RegExp Cheat Sheet

I'll come back to this one tongue.gif
Rich

This post has been edited by rewake: Feb 15 2008, 12:42 PM


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
Jason
post Feb 15 2008, 01:05 PM
Post #4


Master of the Universe
Group Icon

Posts: 1,298
Joined: 15-February 08
From: London, England


The cheat sheet looks very helpful. I will be giving this my full attention over the weekend.

What are the forward slashes for at the beginning and end of the string?


--------------------
Go to the top of the page
 
+Quote Post
rewake
post Feb 15 2008, 01:40 PM
Post #5


Rapid Squeezer
Group Icon

Posts: 239
Joined: 14-February 08
From: NY, USA


The slashes are a delimiter used with perl-compatible regular expressions. They will allow you to add modifiers should you need them. Here's another link so you can learn about regular expressions in PHP...

Regular Expression Functions (Perl-Compatible)

Happy learning! Let me know if you have questions.
Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   2 Jason 199 29th May 2008 - 06:34 AM
Last post by: Jason