Home > Backend Development > PHP Tutorial > Detailed introduction on how to write this regular expression to split strings

Detailed introduction on how to write this regular expression to split strings

PHP中文网
Release: 2023-03-01 07:32:02
Original
1314 people have browsed it

How to write this regular expression to split a string

<pre class="brush:php;toolbar:false">
php > $x="xy\nw";
php > echo strlen($x);4
Copy after login

How to split it into an array of four characters?

Reply content:

str_split
Copy after login

Come on ajs

&#39;xynw&#39;.match(/(.{1})/g)
Copy after login
<?php
$str = &#39;xynw&#39;;
$chars = preg_split(&#39;//&#39;, $str, -1,PREG_SPLIT_NO_EMPTY);
print_r($chars);?>
Copy after login

If you directly want to get a certain character, the string can be accessed directly using an array-like method. If you have other reasons to convert to an array, just ignore me. answer.

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template