Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';在php的文本文件中找不到带引号的单词_Php - Fatal编程技术网

Can';在php的文本文件中找不到带引号的单词

Can';在php的文本文件中找不到带引号的单词,php,Php,我有一个文本文件,我想检查该文件是否包含字符串wor“dddddd。假设这是文本文件的内容: ...,sometext,moretext,wor"dddddd,867767,3468647,sometext,... 这是我的代码,我不知道为什么它不工作(打印8而不是3): 这里有一些错误 首先,在字符串中 $id='wor\"dddddd'; 您不需要在单个带引号的字符串中转义“。因此,您正在尝试查找'wor\'dddddddd'而不是'wor'dddddddd'。很明显,这不会发生 下一个

我有一个文本文件,我想检查该文件是否包含字符串
wor“dddddd
。假设这是文本文件的内容:

...,sometext,moretext,wor"dddddd,867767,3468647,sometext,...
这是我的代码,我不知道为什么它不工作(打印8而不是3):


这里有一些错误

首先,在字符串中

$id='wor\"dddddd';
您不需要在单个带引号的字符串中转义
。因此,您正在尝试查找
'wor\'dddddddd'
而不是
'wor'dddddddd'
。很明显,这不会发生

下一个错误来自使用
htmlspecialchars

此函数将把引号转换为html实体,因此您将使用
wor“dddddd
而不是
wor”ddddddd
。同样,当您有
wor”dddddd
时,您可以找到
wor”ddddddd

因此,解决方案是:

$id = 'wor"dddddd';
$content= file_get_contents("file.txt");
// following lines remain unchanged
奖励:您可以用一行替换循环
$split

$pos = array_search($id, $split);

这里有一些错误

首先,在字符串中

$id='wor\"dddddd';
您不需要在单个带引号的字符串中转义
。因此,您正在尝试查找
'wor\'dddddddd'
,而不是
'wor'dddddddd'
。显然,这不会发生

下一个错误来自使用
htmlspecialchars

此函数将把引号转换为html实体,因此您将拥有
wor“ddddddd
而不是
wor”ddddddd
。同样,当您拥有
wor“ddddddd
时,您可以找到
wor”ddddddddd

因此,解决方案是:

$id = 'wor"dddddd';
$content= file_get_contents("file.txt");
// following lines remain unchanged
奖励:您可以用一行替换循环
$split

$pos = array_search($id, $split);

不需要在单个qouted字符串中转义
。不需要在单个qouted字符串中转义