views:

720

answers:

2

Hi,

I am creating an Image Search Engine. Currently, my script does bold the matching words on the keyword. But, some keywords are too long just like the followings.


When I search for shah rukh khan, there is an image with the following keyword.

25216d1235653089 shahrukh khan s wallpaper shah rukh actor

As you see, the above keyword is too long. So, I need it to be like the following one.

shahrukh khan s wallpaper

| or |

shah rukh actor


Currently, I am using the following code but it requires a space before and after the bold tags. So, if the 1st word is bold, it displays the whole keyword.

if(strlen($img_keyword)>30){
    $img_keyword = preg_replace('/(.*?) <b>(.*?)<\/b> (.*?)/us'," <b>$2</b> ",$img_keyword);
}

Is there a way to do/fix this?

Thank you, pnm123

A: 

Well, as far as I can see, you don't need the spaces in front and after the bold text, so why don't you just remove the space, add a start anchor at the beginning and an end anchor at the end of the pattern? Like this:

$img_keyword = preg_replace('/^(.*?)<b>(.*?)<\/b>(.*?)$/us'," <b>$2</b> ",$img_keyword);
Franz
Thank's for the answer. Now, when I search, it only displays the first mathcing word as the keyword. Ex: When I search for 'Shah Rukh Khan', keyword of the image is **shah**
pnm123
I also tried this **'/^(.*?)<b>(.*?)<\/b>(.*?)/us'** and some image keywords are ok. But, some very long keywords like this **khan %2Cposttag khan died on 27 april%2Cposttag is no more%2Cposttag the style icon of bollywood** are same...
pnm123
Sorry. Remove the u modifier. Like this: $img_keyword = preg_replace('/^(.*?)<b>(.*?)<\/b>(.*?)$/s'," <b>$2</b> ",$img_keyword);
Franz
A: 

After Franz's help, I edited my code as I mentioned below.

$img_keyword = boldText($query, $img_keyword); if(strlen($img_keyword)>30){ $img_keyword = preg_replace('/^(.?)(.?)<\/b>(.*?)/u'," $2 ",$img_keyword); } if(strlen($img_keyword)>30){ $img_keyword = preg_replace('/<\/b>(.*?)/x'," ",$img_keyword); } if(strlen($img_keyword)>30){ $img_keyword = preg_replace('/<\/b>(.?)$|(.?)$/x'," ",$img_keyword); }


If the keywords is

kajol shah rukh vogue india blumarine

the result is

shah rukh


If the keyword is

p 18 mFEk4J448M labelsadt 0%2Clanguage en%2Cposttag feroz khan%2Cposttag khan died on 27 april%2Cposttag is no more%2Cposttag the style icon of bollywood

the result is

khan khan


Is there a way to keep one or two unbold words and remove others?

Thanks you, pnm123

pnm123