-5

如何使用 php 拆分字符串中的单词并存储到两个不同的变量 $str1 , $str2

Input String : The encyclopedia project Wikipedia is the most famous wiki on the public web, but there are many sites running many different kinds of wiki software. Wikis can serve many different purposes both public and private, including knowledge management, notetaking, community websites and intranets. Some permit control over different functions (levels of access). For example, editing rights may permit changing, adding or removing material. Others may permit access without enforcing access control. Other rules may also be imposed to organize content.<div class="new">Some Text Here! </div> 

Output String : 

$str1 : The encyclopedia project Wikipedia is the most famous wiki on the public web, but there are many sites running many different kinds of wiki software. Wikis can serve many different purposes both public and private, including knowledge management, notetaking, community websites and intranets. Some permit control over different functions (levels of access). For example, editing rights may permit changing, adding or removing material. Others may permit access without enforcing access control. Other rules may also be imposed to organize content.

$str2 : <div class="new">Some Text Here! </div>

谢谢

4

2 回答 2

2

首先,您可以使用 strpos 检查要拆分的部分从哪里开始。所以在这种情况下

$pos = strpos($string,"<");

然后你会想要使用 substr 函数来相应地拆分它。

$str1 = substr($string,0,$pos); //This takes everything from the start to the position indicated

$str2 = substr($string,$pos); //This takes everything from the position to the end of the string
于 2013-07-12T14:12:11.767 回答
0

所以找到第一个的<div位置

$divpos = strpos(strtolower($InputString),"<div");

我用strtolower的情况是你得到<DIV而不是<div

然后,使用 substr 就可以了:

$str1 = trim(substr($InputString,0,$divpos));
$str2 = trim(substr($InputString,$divpos));

我曾经trim删除任何不需要的空格。

于 2013-07-12T14:15:23.190 回答