php substr-如何查找<;br>;在启动substr之前?
我成功地使用了这个php substr-如何查找<;br>;在启动substr之前?,php,Php,我成功地使用了这个 $str = "{source}do_something"; if(substr($str, 0, 8) == "{source}") 但是,如果我已经 $str = "<br><br>{source}do_something"; 现在我需要同样的东西,但是有换行符,所以浏览器实际上渲染换行符,然后运行函数 $startTag = "{source}"; $endTag = "{/source}"; $strpos = strpos($str, $
$str = "{source}do_something";
if(substr($str, 0, 8) == "{source}")
但是,如果我已经
$str = "<br><br>{source}do_something";
现在我需要同样的东西,但是有换行符,所以浏览器实际上渲染换行符,然后运行函数
$startTag = "{source}";
$endTag = "{/source}";
$strpos = strpos($str, $startTag);
if ($strpos !== false)
{
$start = $strpos + strlen($startTag);
$innerText = substr($str, $start, strpos($str, $endTag)-$start);
list($functionName, $parameters) = explode(";", $innerText);
call_user_func_array($functionName, explode(",", $parameters));
}
我希望我的英语足够好,可以把这一点说清楚
谢谢
使用解决方案进行新编辑
$startTag = "{source}";
$endTag = "{/source}";
$initial_strpos = strpos($str, $startTag);
$final_strpos = strpos($str, $endTag);
if ($initial_strpos !== false) {
$beforeTag = substr($str, 0, $initial_strpos);
echo htmlspecialchars_decode($beforeTag, ENT_QUOTES);
$start = $initial_strpos + strlen($startTag);
$innerText = substr($str, $start, strpos($str, $endTag)-$start);
$funcao = explode(";", $innerText);
call_user_func_array(array_shift($funcao), $funcao);
$aferTag = strpos($str, $final_strpos);
echo htmlspecialchars_decode($aferTag, ENT_QUOTES);
}
这将打印我的换行符并运行我的功能。请参阅strrpos手册:
$startTag = "{source}";
$endTag = "{/source}";
$strpos = strpos($str, $startTag);
if ($strpos !== false)
{
$start = $strpos + strlen($startTag);
$innerText = substr($str, $start, strpos($str, $endTag)-$start);
list($functionName, $parameters) = explode(";", $innerText);
call_user_func_array($functionName, explode(",", $parameters));
}
因此,您可以找到br标签的位置。请参阅strrpos手册: 因此,您可以找到br标记的位置。您可以尝试使用: 在新评论后编辑:
您可以尝试以下方法:
$from = 1 + stripos($str, "{");
$to = stripos($str, "}", $from) - 1;
$your_string = substr($str, $from, $end - $from + 1);
当然,您应该检查这两个索引($from
和$to
)是否正常,但是这个示例应该给您一个想法…您可以尝试使用:
在新评论后编辑:您可以尝试以下方法:
$from = 1 + stripos($str, "{");
$to = stripos($str, "}", $from) - 1;
$your_string = substr($str, $from, $end - $from + 1);
当然,您应该检查这两个索引(
$from
和$to
)是否正常,但是这个示例应该给您一个想法…如果您必须特别检查前8个字符,那么您应该在执行子字符串之前从字符串中删除
:
if (substr(str_replace("<br>", "", $str), 0, 8) == "{source}")
编辑
这个怎么样?它应该获取{source}{/source}
标记之间的文本,并按拆分它们
,然后将第一个条目作为函数名,第二个条目作为函数的参数
$startTag = "{source}";
$endTag = "{/source}";
$strpos = strpos($str, $startTag);
if ($strpos !== false)
{
$start = $strpos + strlen($startTag);
$innerText = substr($str, $start, strpos($str, $endTag)-$start);
list($functionName, $parameters) = explode(";", $innerText);
call_user_func_array($functionName, explode(",", $parameters));
}
如果必须特别检查前8个字符,则应在执行子字符串之前从字符串中删除
s:
if (substr(str_replace("<br>", "", $str), 0, 8) == "{source}")
编辑
这个怎么样?它应该获取{source}{/source}
标记之间的文本,并按拆分它们
,然后将第一个条目作为函数名,第二个条目作为函数的参数
$startTag = "{source}";
$endTag = "{/source}";
$strpos = strpos($str, $startTag);
if ($strpos !== false)
{
$start = $strpos + strlen($startTag);
$innerText = substr($str, $start, strpos($str, $endTag)-$start);
list($functionName, $parameters) = explode(";", $innerText);
call_user_func_array($functionName, explode(",", $parameters));
}
您可以在运行
strip\u tags()
后对字符串进行比较-如果这样对您有效的话。现在还不完全清楚您的最终目标是什么,您是否试图在花括号{
和}
之间提取字符串?确切地说,我的目标是运行一个名在{source}内的函数,如{source}u菜单;垂直,真{/source}您可以在运行
strip_tags()
后比较字符串-如果这样对您有效的话。现在还不完全清楚您的最终目标是什么,您是否试图在花括号{
和}
之间提取字符串?确切地说,我的目标是运行一个名在{source}内的函数,如{source}u菜单;垂直,真{/source}好的,你的回答几乎是正确的,或者说是正确的,但是它缺少初始和最终的,所以我们可以打印之前的内容、函数和之后的内容。我将重新编辑我的问题,让每个人都能理解。谢谢,你的回答几乎是正确的,或者说是正确的,但是它缺少了开头和结尾,所以我们可以打印之前的内容、功能和之后的内容。我将重新编辑我的问题,让每个人都能理解。谢谢