php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60974 request for function similar to strpbrk but with replace features
Submitted: 2012-02-03 20:21 UTC Modified: 2012-02-03 21:12 UTC
From: jonathan at scriptwriterthingy dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: Irrelevant OS: Linux/CentOS
Private report: No CVE-ID: None
 [2012-02-03 20:21 UTC] jonathan at scriptwriterthingy dot com
Description:
------------
---
From manual page: http://www.php.net/function.strpbrk
---

This is a great function I wasn't aware of until recently. I love that it can 
find any character from a list of characters - very useful for filtering data.

Would be even better if there were a similar function that replaced any matched 
character listed in the input string with: 

1) a single specified character, 
2) a character from a list/string of characters (similar to what you do with 
str_replace but without the need for arrays)
3) a random character from the supplied list of characters mentioned in item 2.

#1 on that list is the most important of my request. #2 and 3 would be nice to 
have but I can live without as str_replace already does some of this but 
requires arrays.

Test script:
---------------
Partial demo of concept

$char_list = "-!@#$%^&*()+=-/?.>,<;:"; //characters we want to remove
$input_string = "Some data that's not supposed to have $puncuation!";
$replace_string = '';
$result_string = strpbrk_replace($input_string, $char_list, $replace_string);




Expected result:
----------------
And hopefully the result string looks like this:

"Some data that's not supposed to have puncuation"

Or if $replace_string had = ' ';
"Some data that's not supposed to have  puncuation "


Actual result:
--------------
See above...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-02-03 20:28 UTC] rasmus@php.net
This sounds like strtr() to me
 [2012-02-03 20:28 UTC] rasmus@php.net
-Status: Open +Status: Analyzed
 [2012-02-03 20:45 UTC] jonathan at scriptwriterthingy dot com
"This sounds like strtr() to me"

According to the php docs strstr says it "Returns part of haystack string from 
the first occurrence of needle to the end of haystack." - it doesn't say 
anything about replacing characters which is the main difference between my 
request and the current strpbrk() function.

My request does have some similarities to str_replace() as mentioned above but 
would do so strings instead of arrays.
 [2012-02-03 21:05 UTC] rasmus@php.net
I said strtr() not strstr()
 [2012-02-03 21:11 UTC] jonathan at scriptwriterthingy dot com
Ah, somehow I missed that. Thanks, I think that's pretty much what I was hoping 
for.
 [2012-02-03 21:11 UTC] jonathan at scriptwriterthingy dot com
-Status: Analyzed +Status: Closed
 [2012-02-03 21:12 UTC] rasmus@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 16:01:36 2025 UTC