Home  >  Article  >  Backend Development  >  Can php only display letters and numbers?

Can php only display letters and numbers?

青灯夜游
青灯夜游Original
2022-04-26 19:11:012248browse

php can filter strings and only display letters and numbers. Implementation method: 1. Use "preg_match_all("/[a-zA-Z0-9]/u", "$str", $arr)" to find letters and numbers and store them in the array; 2. Use "join ('',$arr[0])" converts the array into a string.

Can php only display letters and numbers?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php can filter strings, only Display letters and numbers.

In PHP, you can use the preg_match_all() function with regular expressions to filter strings and only retain English letters and numbers.

The regular expression used is: /[a-zA-Z0-9]/u

##preg_match_all() function accepts an array The third parameter of the type is used to store all matching results.

<?php
header("Content-type:text/html;charset=utf-8");
$str = "php.cn?php中文网。zblog,?#$%^&())*(&^";
preg_match_all("/[a-zA-Z0-9]/u","$str",$arr);
var_dump($arr);
?>

Can php only display letters and numbers?

The array is a two-dimensional array, you can use the join() function to splice the result values ​​into a string

join(&#39;&#39;,$arr[0])

Can php only display letters and numbers?

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of Can php only display letters and numbers?. For more information, please follow other related articles on the PHP Chinese website!

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