Implementation steps: 1. Use the preg_match_all() function with regular expressions to filter strings and retain only English letters. The syntax is "preg_match_all("/[a-zA-Z]/u","$str ",$arr);", the letters will be stored in the "$arr" array; 2. Use the implode() function to splice the result value into a new string, the syntax is "implode($arr[0])".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the preg_match_all() function and implode() function to make the string contain only letters.
Implementation steps:
Step 1: Use the preg_match_all() function with regular expressions to filter strings and only retain English letters
The preg_match_all() function matches all occurrences of the pattern in the string.
preg_match_all (pattern , subject ,matches,flags,offset);
pattern:: The pattern to be searched, in string form.
subject: Input string.
matches: multi-dimensional array, output all matching results as output parameters, array sorting is specified by flags.
#flags: Array result sorting, can be used in combination with the following flags
offset: Usually, search starts from the beginning of the target string position starts. (Unit is bytes)
<?php header("Content-type:text/html;charset=utf-8"); $str = "php.cn23v457zblog,?#$%^&())*(&^"; echo "原字符串:"; var_dump($str); preg_match_all("/[a-zA-Z]/u","$str",$arr); var_dump($arr); ?>
As you can see, the result is a two-dimensional array.
The required English letters are in the internal sub-array $arr[0]
, just convert it into a string.
Step 2: Use the implode() function to splice the result value into a string
The implode() function can convert a one-dimensional array into a string. The syntax is as follows:
implode($glue,$arr)
Description | |
---|---|
$glue | Optional. Used to set a string, indicating that $glue is used to connect each element of the array together. By default, $glue is an empty string.|
$arr | Required. Arrays to be combined into strings.
<?php header("Content-type:text/html;charset=utf-8"); $str = "php.cn23v457zblog,?#$%^&())*(&^"; echo "原字符串:"; var_dump($str); preg_match_all("/[a-zA-Z]/u","$str",$arr); var_dump($arr); $newStr=implode($arr[0]); echo "处理后的字符串:"; var_dump($newStr); ?>
PHP Video Tutorial"
The above is the detailed content of How to keep only letters in php string. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor
