限制回声,以获得所需的字符
问题描述:
$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]
。有没有一种方法可以指定范围/其他方法?
答
如果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 –
这取决于。如果你事先知道'[]'中的文本,你可以使用['str_replace()'](http://php.net/manual/en/function.str-replace.php),否则你必须使用['preg_replace()'](http://php.net/manual/en/function.preg-replace.php) – fusion3k