我正在处理一个恼人的数据库,其中一个字段包含两个单独字段应该存储的内容.所以列存储的内容类似于"第一个字符串〜@〜第二个字符串",其中"〜@〜"是分隔符.(再次,我没有设计这个,我只是想解决它.)
我想要一个查询将其移动到两列,看起来像这样:
UPDATE UserAttributes
SET str1 = SUBSTRING(Data, 1, STRPOS(Data, '~@~')),
str2 = SUBSTRING(Data, STRPOS(Data, '~@~')+3, LEN(Data)-(STRPOS(Data, '~@~')+3))
Run Code Online (Sandbox Code Playgroud)
但是我找不到任何等同于strpos的存在.
让我们说我希望在一些针头char之后返回所有字符'x':
$source_str = "Tuex helo babe".
通常我会这样做:
if( ($x_pos = strpos($source_str, 'x')) !== FALSE )
$source_str = substr($source_str, $x_pos + 1);
Run Code Online (Sandbox Code Playgroud)
你知道更好/更聪明(更优雅的方式)吗?
不使用regexp不会使它更优雅,也可能更慢.
不幸的是我们做不到:
$source_str = substr(source_str, strpos(source_str, 'x') + 1);
Run Code Online (Sandbox Code Playgroud)
因为什么时候'x'找不到strpos返回FALSE(而不是-1像JS那样).
FALSE将评估为零,并且第一个字符将始终被切断.
谢谢,
我想获取脚本来搜索$ open_email_msg,不同的电子邮件将有不同的信息,但格式如下.
我还没有真正使用正则表达式,但我想要做的是每当我有它搜索字符串时它会搜索"标题:[标题数据]","类别:[类别数据].我在问因为我觉得不像
strpos($open_email_msg, "Title: (*^)");
Run Code Online (Sandbox Code Playgroud)
甚至会工作.
这只是整个代码的片段,其余部分将信息插入MySQL表中,然后发布到网站上的新闻文章中.
有人可以帮我找到解决方案吗?
严格的电子邮件格式:
新闻更新
标题:文章标题
标签:tag1 tag2
类别:文章类别,第2文章类别
片段:文章片段.
消息:文章消息.图片.更多文字,更多文字.Lorem impsum dolor坐着.
<?php
//These functions searches the open e-mail for the the prefix defining strings.
//Need a function to search after the space after the strings because the subject, categories, snippet, tags and message are constant-changing.
$subject = strpos($open_email_msg, "Title:"); //Searches the open e-mail for the string "Title"
$subject = str_replace("Title: ", "" ,$subject);
$categories = strpos($open_email_msg, "Categories:"); //Searches the open e-mail for …Run Code Online (Sandbox Code Playgroud) strpos功能反向
我想找到一种方法,我可以在其中找到反向的字符位置.例如,最后"e"反向开始计数.
从例子
$string="Kelley";
$strposition = strpos($string, 'e');
Run Code Online (Sandbox Code Playgroud)
它会给我一个位置1.
这是错误:
适用于:PHP 5.2+
警告:strpos()[function.strpos]:第445行/helper.php中的空分隔符
这是该行的代码:
if($src = $img->getAttribute('src') AND strpos($src,$fgParams->get('base')) === false) { // prevents repeat processing
EgivaUtility::profiling('Processing Image SRC: '.$src);
// fix rel paths
$src = EgivaUtility::encode_url(EgivaUtility::makeAbsUrl($origLink,$src));
if($image_details = @getimagesize($src) AND !in_array($image_details[0],array(1,2)) AND !in_array($image_details[1],array(1,2))) {
EgivaUtility::profiling('Image Details: '.print_r($image_details,true));
$title = $img->getAttribute('title');
$alt = $img->getAttribute('alt');
if($fgParams->get('save_img')) { // consider restoring the JPath::clean()
// find image name and extension
$name = $title ? EgivaUtility::stringURLSafe($title) : EgivaUtility::stringURLSafe($alt);
preg_match('#[/?&]([^/?&]*)(\.jpg|\.jpeg|\.gif|\.png)#i',$src,$matches);
$ext = isset($matches[2]) ? strtolower($matches[2]) : '';
if(!$name) $name = isset($matches[1]) ? EgivaUtility::stringURLSafe($matches[1]) : …Run Code Online (Sandbox Code Playgroud) 什么是python相当于:
if (strpos($elem,"text") !== false) {
// do_something;
}
Run Code Online (Sandbox Code Playgroud) 是的我知道.当我们使用multibyte char时,我们应该使用mb_*函数.但是当我们使用strpos时?我们来看看这段代码(保存在utf-8中)
var_dump(strpos("My symbol utf-8 is the €.", "\xE2\x82\xAC")); // int(23)
Run Code Online (Sandbox Code Playgroud)
使用mb_strpos有区别吗?是不是让这项工作成绩相同?毕竟,不是strpos寻找一个字符串(多个字节)?是否有理由使用strpos?
我在stackoverflow上找到了这个例子:
if (strpos($a,'are') !== false) {
echo 'true';
}
Run Code Online (Sandbox Code Playgroud)
但是如何让它搜索两个单词.我需要这样的东西:如果$ a包含单词"are"或"be"或者两者都包含echo"contains";
我试过xor和||
我通过Windows身份验证登录用户,然后将该用户的权限存储在会话变量中.我在数据库中使用分隔的权限存储方法,即:
$rights //retrieved from database
= 'read,edit,delete,admin'
Run Code Online (Sandbox Code Playgroud)
所以我的问题应该是我;
//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 }
Run Code Online (Sandbox Code Playgroud)
要么
//make array of rights
$_SESSION['userrights'] = explode(',',$rights)
//use in_array to find if right is allowed
if (in_array("admin",$_SESSION['userrights'])) { // do the function }
Run Code Online (Sandbox Code Playgroud)
OCD问题的一点,因为我认为差异对于我正在做的事情来说几乎可以忽略不计,但这会更快(使用更少的资源)方法?
除了侮辱我的权利存储方法之外,任何答案都会受到重视!
strpos ×10
php ×8
arrays ×1
joomla1.5 ×1
performance ×1
python-2.7 ×1
regex ×1
ruby ×1
sql ×1
sql-server ×1
string ×1
substr ×1
syntax ×1
utf-8 ×1