Questions tagged [strpos]

PHP method to find the position of the first occurrence of a substring in a string

PHP method to find the position of the first occurrence of a substring in a string

PHP Reference: https://www.php.net/manual/en/function.strpos.php

705 questions
101
votes
15 answers

Using an array as needles in strpos

How do you use the strpos for an array of needles when searching a string? For example: $find_letters = array('a', 'c', 'd'); $string = 'abcdefg'; if(strpos($string, $find_letters) !== false) { echo 'All the letters are found in the…
MacMac
  • 30,042
  • 53
  • 142
  • 217
86
votes
5 answers

Which method is preferred strstr or strpos?

I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ?
johnlemon
  • 18,991
  • 36
  • 111
  • 174
55
votes
4 answers

How to make strpos case insensitive

How can I change the strpos to make it non case sensitive. The reason is if the product->name is MadBike and the search term is bike it will not echo me the link. My main concern is the speed of the code.
EnexoOnoma
  • 7,471
  • 16
  • 82
  • 166
41
votes
7 answers

selecting an array key based on partial string

I have an array and in that array I have an array key that looks like, show_me_160 this array key may change a little, so sometimes the page may load and the array key maybe show_me_120, I want to now is possible to just string match the array key…
sea_1987
  • 2,722
  • 12
  • 41
  • 68
38
votes
5 answers

PHP check if file contains a string

I'm trying to see if a file contains a string that is sent to the page. I'm not sure what is wrong with this code: ?php $valid = FALSE; $id = $_GET['id']; $file = './uuids.txt'; $handle = fopen($file, "r"); if ($handle) { //…
WildBill
  • 7,965
  • 14
  • 54
  • 85
27
votes
5 answers

Simple PHP strpos function not working, why?

Why isn't this standalone code working: $link = 'https://google.com'; $unacceptables = array('https:','.doc','.pdf', '.jpg', '.jpeg', '.gif', '.bmp', '.png'); foreach ($unacceptables as $unacceptable) { if (strpos($link, $unacceptable) ===…
Daniel
  • 285
  • 1
  • 3
  • 4
24
votes
8 answers

How to use strpos to determine if a string exists in input string?

$filename = 'my_upgrade(1).zip'; $match = 'my_upgrade'; if(!strpos($filename, $match)) { die(); } else { //proceed } In the code above, I'm trying to die out of the script when the filename does not contain the text string…
Scott B
  • 35,095
  • 61
  • 148
  • 245
24
votes
6 answers

strpos() with multiple needles?

I am looking for a function like strpos() with two significant differences: To be able to accept multiple needles. I mean thousands of needles at ones. To search for all occurrences of the needles in the haystack and to return an array of starting…
Nikola Obreshkov
  • 1,538
  • 4
  • 18
  • 29
24
votes
7 answers

strpos function in reverse in php

strpos function in reverse I would like to find a method where i can find a character position in reverse.For example last "e" starting count in reverse. From example $string="Kelley"; $strposition = strpos($string, 'e'); it will give me position…
Taps101
  • 921
  • 1
  • 7
  • 20
23
votes
5 answers

php 5 strpos() difference between returning 0 and false?

if(strpos("http://www.example.com","http://www.")==0){ // do work} I'd expect this to resolve as true, which it does. But what happens when I do if(strpos("abcdefghijklmnop","http://www.")==0){// do work} This also passes on php 5 because as far…
rutherford
  • 9,098
  • 13
  • 47
  • 70
20
votes
4 answers

Why is mb_strpos() so considerably slower than strpos()?

I had criticized an answer that suggested preg_match over === when finding substring offsets in order to avoid type mismatch. However, later on the answer's author has discovered that preg_match is actually significantly faster than multi-byte…
19
votes
2 answers

Using regex in a string for strpos()

I want to get the scripts to search the $open_email_msg which different e-mails will have different info but the same format as below. I haven't really used regex much, but what i want to do is whenever i have it to search the string it would…
brinard sweeting
  • 229
  • 1
  • 2
  • 8
18
votes
5 answers

PHP substr after a certain char, a substr + strpos elegant solution?

let's say I want to return all chars after some needle char 'x' from: $source_str = "Tuex helo babe". Normally I would do this: if( ($x_pos = strpos($source_str, 'x')) !== FALSE ) $source_str = substr($source_str, $x_pos + 1); Do you know a…
Marco Demaio
  • 30,990
  • 33
  • 122
  • 155
18
votes
3 answers

SQL Server: any equivalent of strpos()?

I'm dealing with an annoying database where one field contains what really should be stored two separate fields. So the column is stored something like "The first string~@~The second string", where "~@~" is the delimiter. (Again, I didn't design…
Kip
  • 99,109
  • 82
  • 222
  • 258
16
votes
2 answers

strpos() in Ruby?

In PHP there's a useful strpos function that finds the position of first occurrence of a string. Is there a similar way to do this in Ruby?
Mark
  • 57,724
  • 41
  • 114
  • 149
1
2 3
46 47