Jump to content

Common Expression Help - Seeking all Guru


eatfishy

Recommended Posts

I was reading the regular expression tutorial on a few websites and I thought I understand it. When I try to code it out, I was not getting result that I needed. Below is a blog  and I want to search for certain phrase:

 

 

--------Start Blog-----------

 

If you wanna learn how I create this website, you can start by reading some of the material in the below link. This is how I got started on creating website. [weblink]www.htmlcodetutorial.com[/weblink] I can show you what I know.

 

--------End Blog-------------

 

I basically want to search for any phase like [weblink]www.htmlcodetutorial.com[/weblink] so that I can replace it with something like this <a href="www.htmlcodetutorial.com">www.htmlcodetutorial.com</a>. I tried a few expression and it's not working. I'm concern with searching the phrase and I'll get to the replacing epression part later.

 

$occurence=preg_match("|[weblink].[\/weblink]|", $blogcontent, $match);
echo $occurence;
echo $match[0];

 

www.htmlcodetutorial.com

Link to comment
Share on other sites

Duh... forum removed some slashes from the code...

 

<?php
$occurence=preg_match_all("|\[weblink]([^[]*)\[\/weblink]|", $blogcontent, $match);

 

you should have full matches in $match[0] and urls only in $match[1]

Link to comment
Share on other sites

I tried the code and it didn't work. It was returning ARRAY as the value. I decided to take another route around this by creating a function with the help of string position, string length, string find and string replacement to get what I need.

 

Thanks again for the help.

Link to comment
Share on other sites

Getting an array as a result is normal (with preg_match_all, the results will be stored into array index 0) like so:

 

$blogcontent = <<<EOF
--------Start Blog-----------

If you wanna learn how I create this website, you can start by reading some of the material in the below link. This is how I got started on creating website. [weblink]www.htmlcodetutorial.com[/weblink] I can show you what I know.
More text [weblink]www.htmlcodetutorial2.com[/weblink] followed by yet more text.

--------End Blog-------------
EOF;

preg_match_all('#\[weblink]([^[]*)\[/weblink]#', $blogcontent, $matches);
echo '<pre>'.print_r($matches[0], true); // this is the info you seek

 

I suppose you could always use a preg_replace callback to do the replacements:

 

Example:

$blogcontent = <<<EOF
--------Start Blog-----------

If you wanna learn how I create this website, you can start by reading some of the material in the below link. This is how I got started on creating website. [weblink]www.htmlcodetutorial.com[/weblink] I can show you what I know.
More text [weblink]www.htmlcodetutorial2.com[/weblink] followed by yet more text.

--------End Blog-------------
EOF;

function replacement($input){
    return $input[0] = "<a href=\"$input[1]\">$input[1]</a>";
}

$blogcontent = preg_replace_callback('#\[weblink]([^[]*)\[/weblink]#', 'replacement', $blogcontent);
echo $blogcontent;

 

If the weblink tags can be upper or lowercase, you can always throw an 'i' modifier after the closing delimiter to match it in any case (upper or lower).

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.