Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.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
hashtag后面的字符串中的特定于样式的单词-php_Php_Arrays_Find_Styles - Fatal编程技术网

hashtag后面的字符串中的特定于样式的单词-php

hashtag后面的字符串中的特定于样式的单词-php,php,arrays,find,styles,Php,Arrays,Find,Styles,我的场景:我尝试在字符串中的hashtag后面设置一些名称的样式 例如: $string = 'Lorem #Stewie Smith ipsum dolor #Peter Griffin sit amet, consectetuer #Stewie Griffin.'; 首先,我想将这些名称放入如下数组: array( [item 1] [firstname] => 'Peter' [surname] => 'Griffin' [item 2]

我的场景:我尝试在字符串中的hashtag后面设置一些名称的样式

例如:

$string = 'Lorem #Stewie Smith ipsum dolor #Peter Griffin sit amet, consectetuer #Stewie Griffin.';
首先,我想将这些名称放入如下数组:

array(

    [item 1]
    [firstname] => 'Peter'
    [surname] => 'Griffin'

    [item 2]
    [firstname] => 'Stewie'
    [surname] => 'Griffin'

    [item 3]
    [firstname] => 'Stewie'
    [surname] => 'Smith'

)
因此,我可以在数组中循环并检查数据库中是否存在名字和姓氏

数据库数据:

|id |名|姓|

|1 |彼得|格里芬|

|2 |斯图迪|史密斯|

验证之后,我喜欢在字符串中的名字和姓氏周围加一个div

谁知道答案


提前感谢

您需要使用正则表达式:

//Regular expression (explained below)
$re = "/\\#([a-zA-Z]*)\\s([a-zA-Z]*)/"; 

//String to search
$str = "Lorem #Stewie Smith ipsum dolor #Peter Griffin sit amet, consectetuer #Stewie Griffin."; 

//Get all matches into $matches variable
preg_match_all($re, $str, $matches);
$matches
现在是:

Array ( [0] => Array ( [0] => #Stewie Smith [1] => #Peter Griffin [2] => #Stewie Griffin ) [1] => Array ( [0] => Stewie [1] => Peter [2] => Stewie ) [2] => Array ( [0] => Smith [1] => Griffin [2] => Griffin ) ) 将其放入数组:

$names = [];

foreach($matches[0] as $i => $v){
    $names[] = array("firstname" => $matches[1][$i], "lastname" => $matches[2][$i]);
}
现在,
$names
是:

Array ( [0] => Array ( [firstname] => Stewie [lastname] => Smith ) [1] => Array ( [firstname] => Peter [lastname] => Griffin ) [2] => Array ( [firstname] => Stewie [lastname] => Griffin ) ) 排列 ( [0]=>阵列 ( [名字]=>Stewie [姓氏]=>史密斯 ) [1] =>阵列 ( [名字]=>彼得 [姓氏]=>格里芬 ) [2] =>阵列 ( [名字]=>Stewie [姓氏]=>格里芬 ) )

从这里,您可以循环使用此数组,检查数据库,根据需要进行验证,然后对结果数据执行任何操作。

谁知道答案?我愿意!我赢了什么?A非常感谢:你先生赢了“非常感谢”!谢谢!整天编码有时会让你的脑袋有点晕;) Array ( [0] => Array ( [firstname] => Stewie [lastname] => Smith ) [1] => Array ( [firstname] => Peter [lastname] => Griffin ) [2] => Array ( [firstname] => Stewie [lastname] => Griffin ) )