473,666 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strpos - and multiple needles?

Hi!

I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.

So, I can either?
1) run through the string and test using in_array('a', 'b', 'c')
2) run through array and test using strpos( $items[$i]....
Where I find #2 the best.

But is there a function which can do that faster or in a better way?
Dec 28 '07 #1
18 5794
jodleren wrote:
Hi!

I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.
preg_match("[abc]",$subject) ;
You might find reading something on regular expressions useful.
Cheers,
--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Proudly running Debian Linux with 2.6.22-3-amd64 kernel, KDE 3.5.8, and PHP
5.2.4-2 generating this signature.
Uptime: 14:37:42 up 36 days, 53 min, 4 users, load average: 0.40, 0.81,
0.94

Dec 28 '07 #2
jodleren said:
Hi!

I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.

So, I can either?
1) run through the string and test using in_array('a', 'b', 'c')
2) run through array and test using strpos( $items[$i]....
Where I find #2 the best.

But is there a function which can do that faster or in a better way?
So you have one string, and you want to see if each of your elements are
in it individually.

So:

$needles = array("a", "b", "c");
$haystack = "This is a string with a certain number of each character in
it.";

$counts = array();
function countChars($let ter, $key, $arr) {
global $output;
$garbage = explode($letter , $arr[0]);
$arr[1][$letter] = count($garbage)-1;

}
array_walk($nee dles, 'countChars', array($haystack , &$counts));
print_r($counts ); // Array ( [a] =6 [b] =1 [c] =4 )

That's a little cleaner, I suppose. You're still going through a bunch,
but if you're just counting, that will return an array that looks like that.

All the best,
~A!

--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #3
Iván Sánchez Ortega said:
jodleren wrote:
[snip]
preg_match("[abc]",$subject) ;
Oh, see, that's much better than what I did.

You might find reading something on regular expressions useful.
And so, it seems, would I. Thanks!

~A!
--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #4
Iván Sánchez Ortega said:
jodleren wrote:
>Hi!

I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.
preg_match("[abc]",$subject) ;
You might find reading something on regular expressions useful.
Cheers,
That will only tell you if any of the characters are in the string. Do
you have one that will say whether all of them are in the haystack?

--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #5
My Pet Programmer wrote:
>>I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.

preg_match("[abc]",$subject) ;

That will only tell you if any of the characters are in the string. Do
you have one that will say whether all of them are in the haystack?
Are you looking for http://php.net/count_chars ??

What are you trying to do, exactly?

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

MSN:i_********* *************** *@hotmail.com
Jabber:iv****** ***@jabber.org ; iv*********@kde talk.net
Dec 28 '07 #6
Iván Sánchez Ortega said:
My Pet Programmer wrote:
>>>I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.
preg_match( "[abc]",$subject) ;
That will only tell you if any of the characters are in the string. Do
you have one that will say whether all of them are in the haystack?
Are you looking for http://php.net/count_chars ??

What are you trying to do, exactly?
Oh, for me it was just idle curiosity. I'm not sure what the OP is after.

~A!

--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #7
On Dec 28, 4:43*pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
My Pet Programmer wrote:
>I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.
preg_match("[abc]",$subject) ;
That will only tell you if any of the characters are in the string. Do
you have one that will say whether all of them are in the haystack?
Are you looking forhttp://php.net/count_chars??
What are you trying to do, exactly?
To check for invalid characters in a filename

WBR
Sonnich
Dec 28 '07 #8
jodleren said:
On Dec 28, 4:43 pm, Iván Sánchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
>My Pet Programmer wrote:
>>>>I was reading php.net for a way to find a number of characters in a
strings. Say I want to look for a b and c ind $string.
As far as I could see, there is no function for that.
preg_match ("[abc]",$subject) ;
That will only tell you if any of the characters are in the string. Do
you have one that will say whether all of them are in the haystack?
Are you looking forhttp://php.net/count_chars??
What are you trying to do, exactly?
To check for invalid characters in a filename

WBR
Sonnich
preg_match("*[abc]*", $haystack); worked for me.

~A!

--
Anthony Levensalor
an*****@mypetpr ogrammer.com

Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. - Albert Einstein
Dec 28 '07 #9
jodleren wrote:
To check for invalid characters in a filename
Escape the filename, then.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

MSN:i_********* *************** *@hotmail.com
Jabber:iv****** ***@jabber.org ; iv*********@kde talk.net
Dec 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1977
by: yawnmoth | last post by:
say i have the following php script: <? if (!is_int(strpos("a","aaa"))) // if (strpos("a","aaa") === false) print "no match"; else print "match"; ?> when i run it, i am told there is no match, when there actually is.
6
4288
by: Ben Hallert | last post by:
Hi guys, I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable number of select-multiple inputs on it. Based on the number of variables passed through a GET string, I want to multiply the total number of selected items for each together to see how many possible combinations the selected items are generating. The following snippet of code...
2
1844
by: ralphNOSPAM | last post by:
I'm trying to use strpos to find these xml tags so I can pull out the text data; but the bracket chars won't let the function work. Is there another way to do this? $php_XML_tag = "<codeFacility>ZNY</codeFacility>"; $pos = strpos($php_XML_tag, '<codeFacility>'); print $pos; results is $pos == 0
3
2047
by: julian_m | last post by:
file AddHTML.htm: ---------------------------- This line is <b>ok</b><br> This line is <b>ok</b><br> This line is <b>ok</b><br> <?PHP echo "This one has code prohibited "; ?> This line is <b>ok</b><br> This line is <b>ok</b><br> ----------------------------
2
1783
by: drec | last post by:
I am trying to right an if statment using strpos to determine if the string exists in the variable, however I seem to be getting the wrong effect. Here is my script. <?php $dn = "ABC-DEF"; $pos = strpos( $dn, "ABC-DEF" ); if ( $pos != FALSE ) {
1
1437
by: Dana Cartwright | last post by:
At php.net, the following paragraph is the first thing in the definition of strpos: "Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos() before PHP 5, this function can take a full string as the needle parameter and the entire string will be used." I find the reference to "strrpos" confusing, coming this early in the definition. This is the definition of strpos, not strrpos. ...
2
1767
by: daschicken | last post by:
Hi, ive written the following php code. its a part of an admin form to add a text. when adding the text %bild the script is supposed to swap that with the link to the picture already stored in the mysql db. it works perfectly fine until the $id variable reaches "100". I dont understand why it works for every number from 1 till 99 but not for a bigger one than 100. I mean $id is not fixed to two digits. $text = $_POST;
3
1402
by: Tarik Monem | last post by:
Hi, here I am again :-) I'm trying to compare the contents of two arrays and if there's a match, then I would like to place the contents of that one index of the array into a "new" third array. Here's the code: $myComboVar = array(); for($someVar=0;$someVar<count($myTempArray);$someVar++) {
1
1260
by: Michael Sharman | last post by:
Not quite sure what's happening here, the docs for strpos() say: "Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE." Looks fine, I can simply do: if(strpos("michael", "m") But then the docs go on to say:
0
8449
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8360
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8876
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8784
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8556
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2774
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.