getting words between <b></b> as tag cloud

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
uyewq
Forum Newbie
Posts: 22
Joined: Thu Sep 17, 2009 6:21 am

getting words between <b></b> as tag cloud

Post by uyewq »

Hi,
it says Warning: preg_match_all() [function.preg-match-all]: Unknown modifier ']' in ***/regex.php on line 4

can you help me for correcting this script? my goal is getting words between <b></b>, putting commas between these words and display as tag cloud
i am very new for regex. all helps'll be kindly appreciated. regards

Code: Select all

<?php
function do_reg($text, $regex)
{
    preg_match_all($regex, $text, $result, PREG_SET_ORDER);
    for ($matchi = 0; $matchi < count($result); $matchi++) {
        for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
            $result[$matchi][$backrefi];
        } 
    }
}
 
$text ='<b>word1</b> sometext1 sometext2 sometext3 sometext4 <b>word2 word3</b> sometext5 sometext6 <b>word4</b>';
$regex= '<%<b>%[^>]*>(.*?)</%</b>%>';
do_reg($text, $regex);
?>
:?
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: getting words between <b></b> as tag cloud

Post by prometheuzz »

I don't understand what you were trying to do with that regex:

Code: Select all

<%<b>%[^>]*>(.*?)</%</b>%>
What are those %'s doing in there? Shouldn't it be:

Code: Select all

<b>(.*?)</b>
Note that PHP's regex functions expect the regex-string to be delimited:

Code: Select all

$regex = '/<b>(.*?)</b>/';
And lastly, by default the DOT character does not match new line characters (\r and \n) so it will not match this for example:

Code: Select all

<b>
text text
</b>

If you want that to match as well, you need to enable DOTALL matching (the s at the end of the following regex):

Code: Select all

$regex = '/<b>(.*?)</b>/is';
(the 'i' flag is for case insensitive matching)
uyewq
Forum Newbie
Posts: 22
Joined: Thu Sep 17, 2009 6:21 am

Re: getting words between <b></b> as tag cloud

Post by uyewq »

Hi prometheuzz,
becareful with eagles on the rocks :-)

i appreciated your kind code help.
After your help my $regex is : (plaease note that if i don't put \ character before

Code: Select all

/b>/is';
php debugger says unknown modifier b if i don't add \

Code: Select all

$regex = '/<b>(.*?)<\/b>/is';
and complete php code for getting words between <b></b> as tag cloud is:

Code: Select all

function do_reg($text, $regex)
{
    preg_match_all($regex, $text, $result, PREG_SET_ORDER);
    for ($matchi = 0; $matchi < count($result); $matchi++) {
        for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
            $result[$matchi][$backrefi]; [b]echo $result[$matchi][$backrefi];[/b]
        } 
    }
}
 
$text ='<b>word1</b> sometext1 sometext2 sometext3 sometext4 <b>word2 word3</b> sometext5 sometext6 <b>word4</b>';
//$regex= '<%<b>%[^>]*>(.*?)</%</b>%>';
$regex = '/<b>(.*?)<\/b>/is';
do_reg($text, $regex);
I added echo on line 6. Output of echo is (html source code) : <b>word1</b>word1<b>word2 word3</b>word2 word3<b>word4</b>word4

What is wrong with my echo? I read on manual that preg_match_all 2d array function.
So i think i couldn't extract my values in 2d array correctly? Also some weird <b></b> tags that i don't want to be appear

:crazy: Any help is welcome
uyewq
Forum Newbie
Posts: 22
Joined: Thu Sep 17, 2009 6:21 am

SOLVED Re: getting words between <b></b> as tag cloud

Post by uyewq »

After googleing annoying but useful hours, i found solution for my newbie question.
I hope it will be useful for other newbie php coders. If you found any fault please let newbies like me know
Regards :)

Output of the php code below is:
total 6 values will be printed with comma-seperated form.
boldword00, boldword01, boldword02, boldword03 boldword04, boldword05, boldword06,

Code: Select all

<?php
// some string with special characters
$str = 'Let\'s & . ( )<b>boldword00</b> [] ~ Â @ \\ //  >£#$½{ !^ %&/()= find the stuff <b>boldword01</b> these <b>boldword02</b> two previous <b>boldword03   boldword04</b> brackets <b>boldword05</b> alice mike <b>boldword06</b>';
//the regex
$do = preg_match_all("/<b>(.*?)<\/b>/", $str, $matches);
//count the num of values; 2nd dimension of preg_match_all
echo 'total '.count($matches[1]).' values will be printed with comma-seperated form.'."<br>\n";
    foreach ($matches[1] as $value) {
        echo $value.', '; }
?>
Post Reply