Home  >  Article  >  Backend Development  >  What to do if php string splitting fails

What to do if php string splitting fails

藏色散人
藏色散人Original
2023-01-20 09:56:273385browse

Solution to PHP string segmentation failure: 1. Open the corresponding PHP code file; 2. Use the "str_replace()" function to replace the tab key in the file; 3. Use "explode("_" ,$line);" method can be used to split the string.

What to do if php string splitting fails

The operating environment of this tutorial: Windows 10 system, PHP version 8.1, DELL G3 computer

What to do if php string segmentation fails ?

Solution to the problem that the explode() function cannot split the tab key in php

I encountered a problem a few days ago: it requires reading each character in the file One row, and split each column into an array, where each column is separated by the tab key ("\t").

It is natural to think of using fgets() to read each line in the file and split it using the explode() function.

$f=fopen("xxx","r");
$line=fgets($f);
$line=explode("\t",$line);

I found that the expected effect was not obtained. The explode() function cannot split a string with the tab key as the separator.

Later I thought of a way, use str_replace() function to replace the tab key in the file, and then use explode() to split it.

$f=fopen("xxx","r");
$line=fgets($f);
$line=str_replace("\t","_",$line);
$line=explode("_",$line);

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What to do if php string splitting fails. 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