You are here: Re: Splitting strings, explode? « All PHP « IT news, forums, messages
Re: Splitting strings, explode?

Posted by Stefan Rybacki on 08/26/05 23:31

mike wrote:
> I have variables values in the format of names such as this
>
> $name = "Jack Jones";
> $name = "Peter Barlow";
> $name = "Lisa, Marie, Jenny & Kevin";
> $name = "Michael Marks & Luke Skywalker";
> $name = "Bob & Frank";
>
> All these vars are seperate in their own include which is loaded into a
> containing template. I have a site search, and I basically want to be able
> to search the site for each person. ie, if the $name is "Jack Jones". I
> simply do this:
>
> <a href="search.php?q=<?php print($name); ?>">link</a>
>
> Tha works just fine, however, when the $name variable contains more that one
> person this doesn't work, simply because say "Bob & Frank" only appear
> together once, search.php?q=%22Bob+%26+Frank%22 searches for them together.
> So if eith Bob or Frank appear somewhere else on their own or with another
> person it won't find them.
>
> I want the search to be for [ "bob" "frank" ] NOT [ "bob & frank" ] because
> my search won't match the string. Therefore, I need to be able to break
> apart the $name variable if it contains more than one persons. I figure it
> shouldn't be that hard considering I can break about the variables that
> contain " & " and ", " but so far I'm getting in a bit of a muddle. So I
> guess my question is, I do I get the above variables into these as easily as
> possible?
>
> "Jack Jones" -> "Jack Jones"
> "Peter Barlow" - > "Peter Barlow"
> "Lisa, Marie, Jenny & Kevin" -> "Lisa" "Marie" "Jenny" "Kevin"
> "Michael Marks & Luke Skywalker" -> "Michael Marks" "Luke Skywalker"
> "Bob & Frank" -> "Bob" "Frank"
>

Use explode as you already said.

$names=explode(",",str_replace("&",",",$search));

run thru this array ($names) with foreach

foreach($names as $name) {
$name=trim($name);

//do your search
}


Stefan

>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация