2016-03-11 49 views
1

$response變種有一個名爲custom_test_name組件,它看起來象下面這樣:限制回聲,以獲得所需的字符

[Test_Name]ad.no.check1.check2.check3 

,這裏是小PHP代碼:

<?php 
    echo "<class="."com.tests.".$response["custom_test_name"][1]."</class>"; 
?> 

這版畫<class=com.tests.[</class> ..檢查第一個字符[custom_test_name和類似的回顯["custom_test_name"][2]打印T,[3]打印e ....但是,如何在這種情況下只打印/回顯具體細節?例如。只是迴應這ad.no.check1.check2.check3並消除了那[Test_Name]。有沒有一種方法可以指定範圍/其他方法?

+1

這取決於。如果你事先知道'[]'中的文本,你可以使用['str_replace()'](http://php.net/manual/en/function.str-replace.php),否則你必須使用['preg_replace()'](http://php.net/manual/en/function.preg-replace.php) – fusion3k

回答

1

如果custom_test_name總是要開始[TEST_NAME]你可以像

$trimmed = str_replace('[Test_Name]', '', $response->custom_test_name); 
echo "<class="."com.tests." . $trimmed . "</class>"; 

的東西刪除它。如果它不總是將是這樣的,但將會與[東西]開始,你可以使用類似

$trimmed = preg_replace("/(\[.*\])/", '', $response->custom_test_name); 
echo "<class="."com.tests." . $trimmed . "</class>"; 
+0

是的,custom_test_name總是以[Test_Name]開頭,所以第一種方法起作用。謝謝@Rodrigo –

相關問題