Home  >  Article  >  Backend Development  >  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中文网
PHP中文网Original
2017-03-30 14:40:211202browse

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

How to split it into an array of four characters?

Reply content:

str_split

Come on ajs

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

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.

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