【问题标题】:Extracting a string starting with # or @ php提取以#或@php开头的字符串
【发布时间】:2014-07-09 00:33:40
【问题描述】:
function extractConnect($str,$connect_type){

    $connect_array = array();
    $connect_counter = 0;
    $str = trim($str).' ';

    for($i =0; $i<strlen($str);$i++) {
        $chr  = $str[$i];
        if($chr==$connect_type){          //$connect_type  = '#' or '@' etc
            $connectword = getConnect($i,$str);
            $connect_array[$connect_counter] = $connectword;
            $connect_counter++;
        }
    }
    if(!empty($connect_array)){
        return $connect_array;
    }
}
function getConnect($tag_index,$str){       
    $str = trim($str).' ';
    for($j = $tag_index; $j<strlen($str);$j++) {
        $chr  = $str[$j];
        if($chr==' '){
            $hashword = substr($str,$tag_index+1,$j-$tag_index);                
            return trim($hashword); 
        }

    }
}
$at = extractConnect("#stackoverflow is great. @google.com is the best search engine","@");
$hash = extractConnect("#stackoverflow is great. @google.com is the best search engine","#");

print_r($at);
print_r($hash);

此方法的作用是从字符串中提取#@ 并返回这些单词的数组。 例如输入#stackoverflow is great. @google.com is the best search engine 并输出这个

Array ( [0] => google.com ) Array ( [0] => stackoverflow )

但似乎这种方法很慢,有什么替代方法吗?

【问题讨论】:

  • U 可以用所有这些字符(# 或 @..)创建一个数组,然后检查 in_array($characters, $yourArrayWithData);你的方法似乎!!!方式复杂!!!。这是一个想法,现在没有时间写答案。
  • 但是@Xatenev 我没有任何特定的字符!可以有任何......我只有@#
  • @HackerManiac:会是什么角色?
  • 是的,所以你只需检查 in_array($characters,$yourstringwithdata) 之后,当它在那里时,你就可以接受这个词
  • @Xatenev #xantev #hackermanic #stackoverflow 等等!

标签: php arrays string hash extract


【解决方案1】:

这似乎对我有用。为它提供你想要的符号。

function get_stuff($str) {
    $result = array();
    $words = explode(' ', $str);
    $symbols = array('@', '#');
    foreach($words as $word) {
        if (in_array($word[0], $symbols)) {
            $result[$word[0]][] = substr($word, 1);
        }
    }
    return $result;
}

$str = '#stackoverflow is great. @google.com is the best search engine';

print_r(get_stuff($str));

这会输出Array ( [#] =&gt; Array ( [0] =&gt; stackoverflow ) [@] =&gt; Array ( [0] =&gt; google.com ) )

【讨论】:

  • 我想要 # 和 @ 分开! @mts7
  • 想要运行一次函数但返回 2 个数组!! return array("@"=&gt;$hashed_words,"#"=&gt;$at_words)
  • @HackerManiac 看到我的回答,我想我已经完成了你想要的。
  • @HackerManiac 更新为您提供一个数组,其中包含其他 2 个数组。
【解决方案2】:

你可以这样做:

<?php

function extractConnect($strSource, $tags) {
    $matches = array();
    $tags = str_split($tags);
    $strSource = explode(' ', $strSource);
    array_walk_recursive($strSource, function(&$item) {
        $item = trim($item);
    });
    foreach ($strSource as $strPart) {
        if (in_array($strPart[0], $tags)) {
            $matches[$strPart[0]][] = substr($strPart, 1);
        }
    }
    return $matches;
}

var_dump(extractConnect(
    "#stackoverflow is great. #twitter is good. @google.com is the best search engine",
    "#@"
));

输出:

【讨论】:

  • 我想要一个单独的数组用于#@ 但只想运行一次函数!!
  • latheesan 它有效,但我接受了@AmalMurali 的回答,因为它非常简短易懂!
【解决方案3】:

您可以使用正则表达式来实现:

/<char>(\S+)\b/i

解释:

  • / - 起始分隔符
  • &lt;char&gt; - 您要搜索的字符(作为函数参数传递)
  • (\S+) - 任何非空白字符,一次或多次
  • \b - word boundary
  • i - case insensitivity modifier
  • / - 结束分隔符

功能:

function extractConnect($string, $char) {
    $search = preg_quote($char, '/');
    if (preg_match('/'.$search.'(\S+)\b/i', $string, $matches)) {
        return [$matches[1]]; // Return an array containing the match
    }
    return false;
}

使用您的字符串,这将产生以下输出:

array(1) {
  [0]=>
  string(10) "google.com"
}
array(1) {
  [0]=>
  string(13) "stackoverflow"
}

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 1970-01-01
    相关资源
    最近更新 更多