1

Instagramのキャプションを文字列に保存しました

何かのようなもの:

$caption_text ="This is a beautiful photo #beautiful #photo #awesome #img";

私の目標は、タグを含む1つの配列に文字列を分割し、残りの文字列を変数に保持することです

例えば

$matches[0] --> "#beautiful"
$matches[1] --> "#photo" etc..

also $leftoverString="This is a beautiful photo";

どんな助けでも大歓迎です

4

8 に答える 8

5
$caption_text ="This is a beautiful photo #beautiful #photo #awesome #img";
if (preg_match_all('/(^|\s)(#\w+)/', $caption_text, $arrHashtags) > 0) {
    print_r($arrHashtags[0]);
}
于 2012-09-03T11:59:11.113 に答える
4
$caption_text = "This is a beautiful photo #beautiful #photo #awesome #img";

preg_match_all ( '/#[^ ]+/' , $caption_text, $matches );

$tweet = preg_replace('/#([^ \r\n\t]+)/', '', $caption_text);
于 2012-09-03T11:56:36.357 に答える
1
$temp = explode(' ', $caption_text);
$matches = array();
foreach ($temp as $element) {
    if ($element[0] == '#') {
       $matches[] = $element;
    }
    else
        $leftoverstring .= ' '.$element;
}

print_r($matches);
echo $leftoverstring;
于 2012-09-03T12:07:11.603 に答える
1

あなたは試すことができます:

$caption_text ="This is a beautiful photo #beautiful #photo #awesome #img";

$array = explode(' ', $caption_text);

$photos = array();
foreach ($array as $a) {
    if ($a[0] == '#') {
        $photos[] = $a;
    }
}
于 2012-09-03T11:56:30.990 に答える
1
<?php
$caption_text ="This is a beautiful photo #beautiful #photo #awesome #img";
$new = explode(" ",$caption_text);
foreach($new as $key=>$value)
{
if($value[0] == "#")
$match[] = $value;
else
$rem .= $value." "; 
}
print_r($rem).PHP_EOL;
print_r($match)
?>
于 2012-09-03T12:02:58.693 に答える
1
$caption_text ="This is a beautiful photo #beautiful #photo #awesome #img";

$matches = explode('#',$caption_text);

for($i = 0; $i<count($matches);$i++)
{ 
   $matches[$i]= '#'.$matches[$i];
}

print_r($matches);
于 2012-09-03T11:57:26.763 に答える
1

1 つの可能性は、" " で分解してから、各項目にタグがあるかどうかを確認することです。そうでない場合は、他のものをもう一度文字列にすることができます。例えば:

$arr_text = explode(' ',"This is a beautiful photo #beautiful #photo #awesome #img");
$tmp = array();
foreach ($arr_text as $item) {
    if(strpos($item,'#') === 0) {
        //do something
    } else  {
        $tmp[] = $item;
    }
}
implode(' ', $tmp);

これが役に立ったことを願っています。

于 2012-09-03T11:57:39.227 に答える
0
<?php
$caption_text ="This is a beautiful photo #beautiful #photo #awesome #img";
$new = explode(" #",$caption_text);
print_r($new);
?>
于 2012-09-03T11:53:55.187 に答える