Since you can't use closures, something like:
function pregQuoteBoundary($arg) { return '/\b'. preg_quote($arg) . '\b/'; }
function seolink($prodid) {
...
$linkname = str_replace(' ', '_',
preg_replace(array_map('pregQuoteBoundary', $killarray), ' ',
makeutf8($prodname)));
...
}
will do instead.
On 21/05/2011, at 4:33 PM, Admin wrote:
>
> Thanks for that Simon...
>
> We are currently restricted to PHP 4, and that code is part of a
> function already... the full function is:
>
> function seolink($prodid) { // Crafts an SEO-friendly text-embedded string
> for URL...
> $db = mysql_connect(-stuff-) or die("Could not connect to server!");
> mysql_select_db("-stuff-",$db);
> $prodsql = "SELECT ProdName FROM product WHERE ProdID='$prodid'";
> $prodres = mysql_query($prodsql);
> $prodrow = mysql_fetch_array($prodres);
>
> if($prodrow) { // If we have a valid entry...
> $prodname = $prodrow['ProdName'];
>
> $killarray = array('and', '&', 'the', 'of', '-', ':', '.', '?',
> '\'', '!', '(', ')', '0','1','2','3','4','5','6','7','8','9'); // List of
> undesirables...
> $linkname = str_replace(' ', '_',
> str_replace($killarray,'',makeutf8($prodname))); // Removes undesirables and
> replaces them with underscores...
> while(strpos($linkname,'__') !== FALSE) {
> $linkname = str_replace('__', '_', $linkname); //
> removes multiple consecutive spacers...
> }
> $seo_link = $prodid . "-" . $linkname;
>
> return($seo_link); // Send back final string...
> } else {
> return(''); //... or empty string if there is no listing under
> this ProdID.
> }
> }
>
> So... not too sure how your code would plug in here...
>
>> preg_replace(array_map(function($arg) { return '/\b'.
>> preg_quote($arg) . '\b/'; }, $killarray), ' ', makeutf8($prodname));
>>
>> That uses closures so requires PHP 5.3. If you can't use that, move
>> the function elsewhere, give it a name and pass that name as a
>> string to array_map().
>>
>> What this is doing is using the word boundary class to only match
>> complete words. array_map() just loops over the given array,
>> applies the given function to each item and returns all the return
>> values in an array.
>>
>> On 21/05/2011, at 2:26 PM, Admin wrote:
>>
>>>
>>> Possibly OT, but think this group may have the solution...
>>>
>>> Given a variable of $prodname = "Jane and the Dragon : Fair
>>> Weather Friends";
>>>
>>> And the operation code:
>>> $killarray = array('and', '&', 'the', 'of', '-', ':', '.', '?',
>>> '\'', '!', '(', ')'); $linkname = str_replace(' ', '_',
>>> str_replace($killarray," ",makeutf8($prodname))); // Removes all
>>> surplus non-essential pieces, and inserts http-safe spacers
>>> while(strpos($linkname,'__') !== FALSE) {
>>> $linkname = str_replace('__', '_', $linkname); // Remove multiple
>>> 'spacers' }
>>>
>>> I get a result of $linkname = "Jane_Dragon_Fair_Wear_Friends"
>>>
>>> As you can see, it's stealing the "the" out of "Weather" and
>>> making it "Wear"...
>>>
>>> If I include spaces in the $killarray items, the whole thing
>>> turns even worse, with virtually no _ separators...
>>>
>>> I have tried having 'the ' and ' the' (ditto for other words in
>>> the $killarray too) but then it still does the same thing with
>>> words such as 'anders', 'sand', 'offer', 'theory' and 'menthe'...
>
>
>
> ---
> Karl
>
> --
> NZ PHP Users Group: http://groups.google.com/group/nzphpug
> To post, send email to [email protected]
> To unsubscribe, send email to
> [email protected]
---
Simon Welsh
Admin of http://simon.geek.nz/
Who said Microsoft never created a bug-free program? The blue screen never,
ever crashes!
http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e
--
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]