strpos

How to Find Next String After the Needle Using Strpos()

一曲冷凌霜 提交于 2019-12-02 00:07:34
I'm using PHP strpos() to find a needle in a paragraph of text. I'm struggling with how to find the next word after the needle is found. For example, consider the following paragraph. $description = "Hello, this is a test paragraph. The SCREENSHOT mysite.com/screenshot.jpg and the LINK mysite.com/link.html is what I want to return."; I can use strpos($description, "SCREENSHOT") to detect if SCREENSHOT exists, but I want to get the link after SCREENSHOT, namely mysite.com/screenshot.jpg . In a similar fashion, I want to detect if the description contains LINK and then return mysite.com/link

PHP get everything in a string before underscore

蹲街弑〆低调 提交于 2019-12-01 23:09:29
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 everything before the underscore? $fileinfo['basename'] is equal to 'feature_00' Thanks You should simple use: $imagePreFix = substr($fileinfo['basename'], 0, strpos($fileinfo['basename'], "_")); I don't see any reason to use explode and create extra array just to get first element. You can also use (in PHP 5.3+): $imagePreFix = strstr($fileinfo[

Find whole word with strpos()

戏子无情 提交于 2019-12-01 09:09:31
问题 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 = strpos($mystring, $findme); In this example it would find "a" in the word "a" (as it should do), but also in "beautiful" because there is an a in it. How can i find only whole words and not if it is a part of other words? 回答1: Use regex , with the word

Checking for multiple strpos values

帅比萌擦擦* 提交于 2019-11-30 21:33:54
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 "selected". Examples: Let's say only the number 1 is in the variable, it will echo "selected". Let's say the numbers 1 2 and 3 are in the variable, it will echo "selected." Let's say the numbers 3 9 25 are in the variable, it will echo "selected" (because of that 3!!). Let's say only the number 9 is in the variable, it will NOT echo. Let's say the numbers 9 25 48

Search String and Return Line PHP

走远了吗. 提交于 2019-11-30 21:12:40
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 = $_GET['q']; $homepage = file_get_contents('forms.php'); if(strpos($homepage, "$searchterm") !== false) { echo "FOUND"; //OUTPUT THE LINE }else{ echo "NOTFOUND"; } Just read the whole file as array of lines using file function. function getLineWithString($fileName, $str) { $lines = file($fileName); foreach ($lines as $lineNumber => $line) { if (strpos($line,

in_array vs strpos for performance in php

浪尽此生 提交于 2019-11-30 17:11:28
问题 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 is should I; //generate variable $_SESSION['userrights'] = $rights ($rights is retrieved from database) //use strpos to find if right is allowed if (strpos($_SESSION['userrights'],"admin") !== false) { // do the function } OR //make array of rights $

php端检测是否移动设备的完整代码

社会主义新天地 提交于 2019-11-30 13:01:23
做网页时,需要得到当前打开网页页面的设备是PC的浏览器,还是移动设备的浏览器,再针对不同设备,让页面显示合适内容; 针对这个需求场景,个人认为有三种方法可以实现: 方法一: 通过css ; 方法二,通过js; 方法三 通过 php端; 这三种方法按业务需求,进行采用,会有不一样的效果; css 通过 @media 属性 ,设置不一样屏幕尺寸 显示不一样的页面;达到页面匹配不同尺寸的效果; 通过js 判断设备是移动设备,pc设备、微信客户端等,再做对应的跳转到合适的业务页面展示给客户端; 方式三,通过php端判断 访问的设备是 pc还是移动设备; 后端返回 不一样的业务内容; 个人觉得方式三 适合复杂的,业务需求。 下面就 提供一个 php端 判断设备的代码吧。 /** * 是否移动端访问访问 */ public function _is_mobile() { if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 return true; } elseif (isset ($_SERVER['HTTP_USER_AGENT'])) { // 判断手机发送的客户端标志,兼容性有待提高 $clientkeywords = [ 'nokia', 'sony', 'ericsson',

SQL Server: any equivalent of strpos()?

前提是你 提交于 2019-11-30 07:54:50
问题 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 this, I'm just trying to fix it.) I want a query to move this into two columns, that would look something like this: UPDATE UserAttributes SET str1 = SUBSTRING(Data, 1, STRPOS(Data, '~@~')), str2 = SUBSTRING(Data, STRPOS(Data, '~@~')+3, LEN(Data)-

Problem with Strpos In PHP

做~自己de王妃 提交于 2019-11-29 15:25:20
I'm writing a simple function and for some reason(probably a simple one) it's not working for me and I was wondering if you guys could help me out. function check_value($postID) { $ID = $postID; $cookie = $_COOKIE['list_of_IDS']; $position = strpos($cookie,$ID); echo 'ID:'.$ID.'-Cookie:'.$cookie; if ($position !== false) { echo "ID is in the cookie"; } } In trying to figure out what the problem was I put that echo line above the If Statement there to make sure there actually is stuff in the variables. My problem is that the IF statement never prints out. A $postID is a number 123123 . The

SQL Server: any equivalent of strpos()?

别来无恙 提交于 2019-11-29 05:38:34
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 this, I'm just trying to fix it.) I want a query to move this into two columns, that would look something like this: UPDATE UserAttributes SET str1 = SUBSTRING(Data, 1, STRPOS(Data, '~@~')), str2 = SUBSTRING(Data, STRPOS(Data, '~@~')+3, LEN(Data)-(STRPOS(Data, '~@~')+3)) But I can't find that any equivalent to strpos exists. User charindex: Select