Home >Backend Development >PHP Tutorial > PHP提取字符串中的字符一部分

PHP提取字符串中的字符一部分

WBOY
WBOYOriginal
2016-06-13 13:22:431045browse

PHP提取字符串中的字符部分
$str1 = 'abc12';
$str2 = 'edf3';

如何提取变量$str1和$str2中的只是字符部分(从$str1中提取出'abc',从$str2中提取出'edf')?

------解决方案--------------------

PHP code
[liangdong@bb-browser-test00.vm.baidu.com php_project]$ php main.php 
abcdefg[liangdong@bb-browser-test00.vm.baidu.com php_project]$ cat main.php 
<?php $str = "abc123d45ef6g7";
$n = preg_match_all('/[a-zA-Z]*/', $str, $matches);
if ($n) {
        echo implode("", $matches[0]);
}
?>
<br><font color="#e78608">------解决方案--------------------</font><br>取出全部的字母<br>echo preg_replace('/[^a-z]/i', '', $str1);<br><br>只取前边的字母<br>echo preg_replace('/^([a-z]+).*/i', '$1', $str1); <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn