php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51335 Search first substring from list
Submitted: 2010-03-20 11:45 UTC Modified: 2020-02-13 17:18 UTC
From: vovan-ve at yandex dot ru Assigned:
Status: Suspended Package: Strings related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vovan-ve at yandex dot ru
New email:
PHP Version: OS:

 

 [2010-03-20 11:45 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-11 14:21 UTC] kalle@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: kalle
 [2010-06-11 14:21 UTC] 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-06-11 17:48 UTC] 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-10-21 12:35 UTC] kalle@php.net
-Status: Feedback +Status: Open -Assigned To: kalle +Assigned To:
 [2020-02-13 17:18 UTC] cmb@php.net
-Status: Open +Status: Suspended
 [2020-02-13 17:18 UTC] cmb@php.net
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>
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 04:01:29 2025 UTC