获取String的最后一个序列并删除之前的所有内容

时间:2017-04-05 12:41:39

标签: php string

给定这个字符串:"hal today is a beatiful weather hal whats going on hal super"我希望在“hal”的最后一个char序列之后得到所有内容。在这种情况下:"super"

这条小线已经在第一个"hal"之后切断了所有内容:

$whatIWant = substr($string, strpos($string, "hal ") + 4);

但是我不知道如何实现它,它只需要最后"hal "

7 个答案:

答案 0 :(得分:1)

使用2.4.7获取最后一个位置:

strrpos

答案 1 :(得分:0)

你可以这样做:

$arr = explode('hal ', $your_string);
$result = $arr[1];

答案 2 :(得分:0)

您可以使用explode将带分隔符的字符串拆分为数组并获取最后一个数组条目。

Traceback (most recent call last):
  File "D:\OneDrive\tensornet.py", line 34, in <module>
    model.fit(data, labels, n_epoch=1000, show_metric=True, batch_size=1600)
  File "C:\Python3\lib\site-packages\tflearn\models\dnn.py", line 215, in fit
    callbacks=callbacks)
  File "C:\Python3\lib\site-packages\tflearn\helpers\trainer.py", line 333, in fit
    show_metric)
  File "C:\Python3\lib\site-packages\tflearn\helpers\trainer.py", line 774, in _train
    feed_batch)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
    run_metadata_ptr)
  File "C:\Python3\lib\site-packages\tensorflow\python\client\session.py", line 944, in _run
    % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
ValueError: Cannot feed value of shape (1600,) for Tensor 'TargetsData/Y:0', which has shape '(?, 1)'

答案 3 :(得分:0)

我使用explodeend

// the string you want to scan
$text = "hal today is a beatiful weather hal whats going on hal super";

// get an array containing sequences after each "hal"
$sequences = explode("hal", $s);

// get the last sequence
$last_seq = end($e);

答案 4 :(得分:0)

仅用于通用目的。

function getLast($string, $neddle) {
    $last = strrpos($string, $neddle);
    return $last === false ? "" : substr($string, $last + strlen($neddle));
}
$string = "hal today is a beatiful weather hal whats going on hal super";
print getLast($string, "hal ");

请注意,您需要包含该空格!

答案 5 :(得分:0)

我认为你可以尝试这项工作

$stringdata = 'hal today is a beatiful weather hal whats going on hal super';

$substringdata = 'hal';

echo substr($stringdata, strpos($stringdata, $substringdata)+strlen($substringdata)+1);

答案 6 :(得分:0)

您可以在hal爆炸输入字符串后获取最后一段。或者使用正则表达式来提取内容:

$input = 'hal today is a beatiful weather hal whats going on hal super';

// Using explode
$output = @trim(end(explode('hal', $input))); // 'super'

// Or using regular expression
$output = preg_replace('/^.*hal\s*(.*)$/', '$1', $input); // 'super'