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
5
votes
6 answers

Find part of a string and output the whole string

I would like to find part of a string and if true I want to ouput the whole of the string that it finds. Below is an example: $Towns = "Eccleston, Aberdeen, Glasgow"; $Find = "Eccle"; if(strstr($Find, $Towns)){ echo outputWholeString($Find,…
Connor Simpson
  • 487
  • 1
  • 7
  • 27
5
votes
2 answers

php, strpos extract digit from string

I have a huge html code to scan. Until now i have been using preg_match_all to extract desired parts from it. The problem from the start was that it was extremely cpu time consuming. We finally decided to use some other method for extraction. I read…
Mevia
  • 1,517
  • 1
  • 16
  • 51
5
votes
2 answers

How to use PHP strpos not case sensitive?

I have made a php script that searches a whole directory with text files in it, everything works fine, except that it is case sensitive. How would i search without it being case sensitive? Here's the code i have so far:
Jacoby Yarrow
  • 155
  • 1
  • 13
5
votes
4 answers

strpos returns true always in php

I wrote this code: $token="Birth"; $msg="I am awesome and I know it"; if(strpos(strtolower($msg),strtolower($token)) >=0){ echo 'ok'; } It prints ok As we can see there is no word like Birth in the message but still it returns true. I guess it…
user1765876
5
votes
5 answers

Sort search results from MySQL by the position of the search query in the string using PHP

I want my search results to be in order of string position from smallest to greatest. For example, searching for "banana" returns: Babyfood, plums, bananas and rice, strained Bananas, dehydrated, or banana powder Bananas, raw Bread, banana,…
user248600
5
votes
2 answers

PHP: strpos & substr with UTF-8

Say I have a long UTF-8 encoded string. And say I want to detect if $var exists in this string. Assuming $var is always going to be simple letters or numbers of ascii characters (e.g. "hello123") I shouldn't need to use mb_strpos or iconv_strpos…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
5
votes
4 answers

strpos() within while loop never ends

There's a string, $string = 'Foo, Bar, Test,'; All I want to do is to count the number of the commas within a string. But everything leads to infinite while loop. So, I've tried #1: $count = 0; while($pos = strpos($string, ',') !== FALSE){ …
Yang
  • 8,580
  • 8
  • 33
  • 58
4
votes
3 answers

which is the fast process strpos()/stripos() or preg_match() in php

I just want to known which one will be fast of strpos()/stripos() or preg_match() functions in php.
4
votes
5 answers

str_replace with strpos?

The str_replace function with a strpos checking can avoid extra work? METHOD 1 ... if (strpos($text, $tofind) !== FALSE) $text = str_replace($tofind, $newreplace, $text); ... METHOD 2 ... $text = str_replace($tofind, $newreplace,…
Manz
  • 948
  • 12
  • 26
4
votes
2 answers

why is a strpos that is !== false not true?

Consider the following example: $a='This is a test'; If I now do: if(strpos($a,'is a') !== false) { echo 'True'; } It get: True However, if I use if(strpos($a,'is a') === true) { echo 'True'; } I get nothing. Why is !==false not ===true…
Pete
  • 105
  • 1
  • 9
4
votes
3 answers

Find whole word with strpos()

Hello people :) I'm trying to use strpos() to find whole words in a sentence. But at the moment, it also find the word if it just are a part of another word. For example: $mystring = "It's a beautiful morning today!"; $findme = "a"; $pos =…
Simon Thomsen
  • 1,351
  • 7
  • 27
  • 37
4
votes
1 answer

strpos not working for a certain string

I'm trying to use strpos to find a string inside another string, but for some reason it isn't working for a certain string, even though it's working for the other one. What am I doing wrong?
frosty
  • 2,559
  • 8
  • 37
  • 73
4
votes
4 answers

Use preg_match() to find start/stop positions of a substring matching a pattern

I want to copy a substring of a string using PHP. The regex for the first pattern is /\d\|\d\w0:/ The regex for the second pattern is /\d\w\w\d+:\s-\s:/ Is it possible combining preg_match with strpos to get the exact positions from start to end…
user366121
  • 3,203
  • 9
  • 48
  • 69
4
votes
4 answers

PHP strpos() function comparision of string conflicts in numbers

I am using strpos() function to find the string in an array key members. foreach($_POST as $k => $v){ // for all types of questions of chapter 1 only $count = 0; if(strpos($k, 'chap1') !== false){ $count++; } } I know…
TheManish
  • 183
  • 1
  • 15
4
votes
1 answer

How to return true if strpos is not exactly false?

I know how strpos works, and it is working as expected, but the return in the if function isn't. Some code: foreach ($socialstream as $key => $social) { //filtering in here $result= $this->search_objects($social); .... } My function…
Bananam00n
  • 814
  • 9
  • 27
1 2
3
48 49