FAQ Database Discussion Community


find exact word in a var string in PHP [closed]

php,strpos
i'm trying to find an exact word inside a string. example: $word = "Many Blocks"; if (strpos($word, "Block")){ echo "You found 1 Block"; } if (strpos($word, "Blocks")){ echo "You found many Blocks"; } The problem here is both if are true.. and i need to find only if is the...

Check to see if EXACT value is in a string - PHP

php,preg-match,strpos
I have read numerous posts about how to see if a value is in a string. Apparently my request is different since the multiple bits of code haven't worked 100%. I am searching a string and need to make sure the EXACT value is in there - with a perfect...

Finding a string by looping through an array php

php,arrays,if-statement,foreach,strpos
I have this array $filelist Array ( [0] => . [1] => .. [2] => .DS_Store [3] => 11-96.eml [4] => 11-97.eml ) Which is a list of all files in a particular directory... Sometimes there is no .DS_Store file, sometimes there is, sometimes there are 2 eml files, sometimes...

PHP Search Not Matching Line in File

javascript,php,file-handling,strpos,comparison-operators
Just creating a simple login system for a school club. And I am trying to send an error if the name that is entered does not match any line in the file containing student names. The file looks a bit like this: Student One 8M2 Student Two 8M3 Student Three...

Multiple strpos possible?

php,preg-match-all,strpos
I'm trying to see if my string contains { and : and } if so returns true, but I'm missing something here. $string = "{12345:98765}"; if(strpos($string,'{*:*}')== true) { echo "string is good"; } Ideas?...

PHP strpos match all needles in multiple haystacks

php,arrays,match,strpos
I want to check if ALL words from $words exist in one or more $sentences, word order is not important. Words will only contain [a-z0-9]. Sentences will only contain [a-z0-9-]. My code so far, it almost work as expected: $words = array("3d", "4"); $sentences = array("x-3d-abstract--part--282345", "3d-speed--boat-430419", "beautiful-flower-462451", "3d-d--384967"); foreach...

UTF8 mb_strpos ISSUE

php,utf-8,strpos
I am facing realy strange problem i am trying to find a UTF-8 needle in UTF-8 haystack and it just dont want to work properly. Here is an example. header('Content-Type: text/html; charset=utf-8'); mb_internal_encoding("UTF-8"); vardump(mb_strpos('Příkon', 'Příkon ')); It returns false header('Content-Type: text/html; charset=utf-8'); mb_internal_encoding("UTF-8"); vardump(mb_strpos('Příkon', 'Příkon')); this returns 0 (success) Any...

PHP Grab a certain word and ignore the rest in a string

php,regex,strpos
I am trying to grab only usernames in a list of sentences of about 30 lines long. I am able to get it to grab the usernames and everything afterwards but I dont want to get everything after the username. test.txt [India] Hagiwara has been inactive for 33 days (last...

strpos - user agent partial matching

php,string-matching,strpos
I am trying to detect 2 different phones based on their user agents. I used strpos but my code is outputting "other phone" on both phones. $ua = $_SERVER['HTTP_USER_AGENT']; $phone1 = "SM-G900F"; $phone2 = "GT-I9505"; if(strpos($phone1,$ua)!==false){ echo "gs 5"; } elseif (strpos($phone2,$ua)!==false){ echo "gs 4"; } else { echo "other...

How to find a substring of a string in an array PHP

php,search,strpos
If you look at many of my questions, you will see that sometimes I don't ask the best questions or I am in such a rush that I don't see the answer that is right in front of me the whole time. If this is another one of those questions,...

PHP strpos not working right

php,strpos
EDIT: Ok for everyone who seems to be mentally disabled or something, check this out how it should be and why Halcyon's solution to swap haystack and needle DO MAKE SENSE: $test = 'bla'; if(strpos($test, 'hauptkat') !== false OR strpos($test, 'kat') !== false) { echo 'hauptkat or kat is inside...

How to use PHP to recognising (and not validating) URL

php,url,protocols,strpos
I'm aware of filter_var() and it's FILTER_VALIDATE_URL filter. The point is there are some URLs which are exist but not count as a valid URL and I need to verify them. For example, URLs with spaces. At the moment I am checking only those protocols that application is interested in...

search tags in a php string with strpos

php,strpos
this is an UTF-8 string $string = '<h2> a none english char - utf-8 string </h2>'; I want to check <h2> tag exist in $string or not I try: if(strpos($string , '<h2>')) or if(strpos($string , '<h2>') == true ) or if(strpos($string , '<h2>') === true) or if(mb_strpos($string , '<h2>' ))...

Strange behaviour of strpos and Ø [duplicate]

php,strpos
This question already has an answer here: Simple PHP strpos function not working, why? 5 answers This seems odd to me but maybe there's a simple explanation. Why does the following block of code result in false despite the needle definitely being part of the haystack? if(strpos("&Oslash;25x&Oslash;2", "&Oslash;")){ echo...

vqmod throws strpos() empty needle error

php,opencart,strpos,vqmod
vQmod throws the following error when parsing an XML file: Warning: strpos(): Empty needle in /home/public_html/vqmod/vqmod.php on line 455 Here's the XML itself: <operation> <search position="before"> <?php echo $footer; ?> </search> <add><![CDATA[ <?php phpinfo(); ?> ]]></add> </operation> XML file looks ok, all paths are correct. Didn't find anything relevant on...

Detect Characters In Any Order

php,strpos
I'm sorting a username registration system, and part of our protocol is that users can only use A-Z, a-z, 0-9, _, ., and -. In addition, they can't have two punctuation marks in a row. I'm trying to find an easier way to detect the punctuation other than just writing...

Shorter way to write strpos with multiple needles in the haystack in PHP

php,preg-match,strpos
I'm checking to see if any of the needles /cgi-bin /css etc are in the haystack $dir. In order to check on multiple needles, I have to write the || after each strpos(). I've seen some other threads on here, but none that really showed the most simplest code. I'm...

Get a substring of a string

php,string,substring,substr,strpos
I've been trying to get a substring of a string that contains around 40 lines of text. The string is something like this but with aprox. 30 more lines: Username: example Password: pswd Code: 890382 Key: 9082 type: 1 Website: https://example.com/example Email: [email protected] I need to get the value of,...

String not found although it should

php,strpos
$row['solved']= "12|10|3"; $id=10; $pos = strpos($row['solved'], $id); if ($pos !== false){ echo "String found!"; exit; } echo "String not found!"; Why does this always return "String not found"?...

Remove all zero values from string PHP [duplicate]

php,split,substr,strpos
This question already has an answer here: How to strip trailing zeros in PHP 15 answers I have a string like this: 14522354265300000000000 I want to display it without zero values, how I can do this? I do this $pos = strpos($route, '0'); $length = count(str_split($route)); $a = $length...

What's wrong with this strpos and ternary operator?

php,ternary-operator,strpos
strpos($mydata,'Εntity') === false) ? 'normal' : 'other' It returns always 'normal' even if $mydata="Entity: This data must work." ...