Req #51335 [Opn->Sus]: Search first substring from list
| From: | cmb@php.net | Date: | Thu, 13 Feb 2020 17:18:50 +0000 |
| Subject: | Req #51335 [Opn->Sus]: Search first substring from list | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225563@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=51335&edit=1
ID: 51335
Updated by: cmb@php.net
Reported by: vovan-ve at yandex dot ru
Summary: Search first substring from list
-Status: Open
+Status: Suspended
Type: Feature/Change Request
Package: Strings related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
This appears to be a controversial feature addition, and as such
deserves to be discussed on the PHP internals mailing list[1].
Feel free to post there; for the time being I'm suspending this
ticket.
[1] <https://www.php.net/mailing-lists.php#internals>
Previous Comments:
------------------------------------------------------------------------
[2010-06-11 17:48:17] vovan-ve at yandex dot ru
Yes, such functionality can be done by strpos() foreach needle in the array. But I see at least two
ways to make such search more optimal:
1. Make limit for maximal possible position after which we should not search a needles. The strpos()
has no such limit, so we need to make substr() after we find next nearest needle.
2. Check length of a next needle and remaining substring to search in.
Of course I can write the function, which do what I need, but I think, that it won't more
optimal in PHP code, then build-in PCRE or build-in function I request.
------------------------------------------------------------------------
[2010-06-11 14:21:18] kalle@php.net
I dont think we should add such functionality that already can be done with a few lines of php code
already, combining strpos/stripos with a foreach and you should get your result:
<?php
function strpos_array_impl($case_sensitive, $haystack, Array $needle, &$found = NULL, $position
= 0)
{
if(!sizeof($needle) || empty($haystack))
{
return(false);
}
$strpos_function = ($case_sensitive ? 'strpos' : 'stripos');
foreach($needle as $temp)
{
if(($pos = $strpos_function($haystack, $temp, $found, $position) !== false)
{
return($pos);
}
}
return(false);
}
function strpos_array($haystack, Array $needle, &$found = NULL, $position = 0)
{
return(strpos_array_impl(false, $haystack, $needle, $found, $position));
}
function stripos_array($haystack, Array $needle, &$found = NULL, $position = 0)
{
return(strpos_array_impl(true, $haystack, $needle, $found, $position));
}
?>
Dont take that code for granted, as i wrote it on my cell so it might be buggy, but should show what
I mean
------------------------------------------------------------------------
[2010-03-20 11:45:58] vovan-ve at yandex dot ru
Description:
------------
If we want to search first substrings from list, we should make multiple calls to strpos() for each
substring. Using PCRE will slowdown performance. So, we need some new function like:
int strpos_array(string $haystack, array $needles, string &$found [, int $offset = 0 ])
which will accept array of substrings in $needle, search first position of them. If nothing found
then returns FALSE. If found any substring then assign it to $found and return its position.
The function name is not important. Other functions for case-insensitive search and/or for reverse
search (stripos_array, strrpos_array, strripos_array) also useful.
Extending function strpos() to accept array in $needle is not good due to &$found parameter.
Good example of using such function is writing parser of some data like SQL dump. Performance of the
function should be higher then PCRE.
Test script:
---------------
$string = 'foo;bar::baz-qwe';
$search = array('-', ';', '::');
$pos = strpos_array($string, $search, $found, 5);
if ($pos === false) {
echo "Nothing found";
}
else {
echo "Found '$found' at $pos";
}
Expected result:
----------------
Found '::' at 7
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=51335&edit=1