簡體   English   中英

在其他兩個字符串之間獲取字符串

[英]Get string between two other strings

例如:

$str = "This is my string.";
$startafter = "This is";
$endbefore = ".";

我需要$output = "my sting"; (我將trim()刪除“我”之前的空格。)

如何在PHP中做到這一點? 謝謝。

preg_match("/".preg_quote($startafter)."(.*?)".preg_quote($endbefore)."/", $str, $output_array);

您可以使用preg_replace()php默認方法,例如

<?php

$test = '';
$test = preg_replace("/^$startafter/i",'',$str');
$test = preg_replace("/$endbefore$/i",'',$str');

echo $test;

?>

希望這能解決您的問題或類似的問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM