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

730 questions
16
votes
7 answers

Checking for multiple strpos values

I am wondering how to complete multiple strpos checks. Let me clarify: I want strpos to check the variable "COLOR" to see if any numbers from 1 to 8 are anywhere in the variable. If any numbers from 1 to 8 are present, I want to echo…
Chad Cardiff
  • 459
  • 3
  • 8
  • 23
16
votes
3 answers

What is the python equivalent of strpos($elem,"text") !== false)

What is the python equivalent of: if (strpos($elem,"text") !== false) { // do_something; }
Cosco Tech
  • 565
  • 1
  • 4
  • 22
13
votes
3 answers

mb_strpos vs strpos, what's the difference?

Yes: I know. We should use mb_* function when we're working with multibyte char. But when we're using strpos? Let's take a look this code (saved in utf-8) var_dump(strpos("My symbol utf-8 is the €.", "\xE2\x82\xAC")); // int(23) There is a…
Federkun
  • 36,084
  • 8
  • 78
  • 90
12
votes
4 answers

How do I resolve a strpos() "empty delimiter" error?

Here's the error: For: PHP 5.2+ Warning: strpos() [function.strpos]: Empty delimiter in /helper.php on line 445 and here is the code on that line: if($src = $img->getAttribute('src') AND strpos($src,$fgParams->get('base')) === false) { // prevents…
Jamison
  • 2,218
  • 4
  • 27
  • 31
11
votes
4 answers

Warning: array_push() expects parameter 1 to be array

This is my code, and when I run this function I get this :Warning: array_push() expects parameter 1 to be array However I define $printed as an array prior to starting. $printed = array(); function dayAdvance ($startDay, $endDay, $weekType){ …
Osman
  • 1,771
  • 4
  • 24
  • 47
10
votes
9 answers

strpos with two words to find

I found this example on stackoverflow: if (strpos($a,'are') !== false) { echo 'true'; } But how do I make it search for two words. I need something like this: if $a contains the words "are" or "be" or both echo "contains"; I tried xor and ||
10now
  • 387
  • 2
  • 3
  • 14
8
votes
2 answers

Find first occurence numerical position in a string by php

May I ask how to find a first occurrence numerical position in a string with PHP? For example, if "abc2.mp3" is a string, return a value of 3 since position of the first occurrence number "2" in "abc2.mp3" is 3 (the first position is 0). I used…
CKH
  • 133
  • 9
8
votes
4 answers

PHP get everything in a string before underscore

I have this code here: $imagePreFix = substr($fileinfo['basename'], strpos($fileinfo['basename'], "_") +1); this gets me everything after the underscore, but I am looking to get everything before the underscore, how would I adjust this code to get…
user3723240
  • 395
  • 3
  • 11
  • 29
8
votes
4 answers

in_array vs strpos for performance in php

I am logging in users via windows authentication and then storing that user's rights in a session variable. I use a delimited method of rights storage in a database i.e: $rights //retrieved from database = 'read,edit,delete,admin' so my question…
DJB
  • 257
  • 2
  • 11
8
votes
4 answers

PHP fwrite() how to insert a new line after some specific line

I'm new here. Anyway, I did my research on fwrite(), but I couldn't find solution, so i'm asking for help. What I want is f.e. to add a new line of text after some other specific line. F.e. I have a .txt file in which there is: //Users //Other…
Aneszej
  • 83
  • 1
  • 1
  • 8
7
votes
4 answers

stripos returns false when special characters is used

I am using the stripos function to check if a string is located inside another string, ignoring any cases. Here is the problem: stripos("ø", "Ø") returns false. While stripos("Ø", "Ø") returns true. As you might see, it looks like the function…
foens
  • 8,642
  • 2
  • 36
  • 48
7
votes
2 answers

How to remove all data after last dot using php?

How to remove all data after last dot using php ? I test my code. It's echo just aaa I want to show aaa.bbb.ccc How can i do that ?
mongmong seesee
  • 987
  • 1
  • 13
  • 24
6
votes
5 answers

Search String and Return Line PHP

I'm trying to search a PHP file for a string and when that string is found I want to return the whole LINE that the string is on. Here is my example code. I'm thinking I would have to use explode but cannot figure that out. $searchterm = …
Haru
  • 1,361
  • 2
  • 15
  • 40
6
votes
1 answer

How to find position of a character in a string in PHP

How to find positions of a character in a string or sentence in php $char = 'i'; $string = 'elvis williams'; $result = '3rd ,7th and 10th'. I tried strpos..but no use..
phpdeveloper
  • 81
  • 1
  • 1
  • 3
5
votes
3 answers

Returning a string after the second occurrence of a character and before the last occurrence of the same character with php

I have several strings similar to the following: ORG-000012 – Name of variation – Quantity: 12 Pack – $14.95 I need to remove all characters before the second hyphen and after the last hyphen with php. For example, the string above needs to return…
Christy
  • 191
  • 1
  • 4
  • 12
1
2
3
48 49