PHP implements the solution to intercept Chinese strings without the ? sign_php tips

墨辰丷
Release: 2023-03-28 09:40:02
Original
1225 people have browsed it

This article mainly introduces the solution for PHP to intercept Chinese strings without the ? sign, involving PHP string traversal and encoding conversion and other related operating techniques. Friends in need can refer to it

The details are as follows :

When PHP intercepts a mixed string of Chinese and English, the last Chinese character is often split into two halves, for example: intercepting the first 18 characters of the string

<?php
$text = "1欢迎访问sina新浪播客";
$value = substr($text, 0, 18);
echo $value."<BR>";
?>
Copy after login

The output is:

1欢迎访问新浪?BR>
Copy after login

So I wrote the following code to determine if the Chinese character bytes in a mixed Chinese and English string If the number is an odd number, one less byte will be intercepted to ensure complete display of Chinese characters.

<?php
$text = "1欢迎访问sina新浪播客";
$value = substr($text, 0, 18);
$value_length = strlen($value);
$value_count = 0;
for ($i = 0; $i < $value_length; $i++)
{
  if (ord($value{$i}) > 127)
  {
    $value_count++;
  }
}
if ($value_count % 2 != 0)
{
  $value = substr($text, 0, $value_length - 1);
}
echo $value."<BR>";
?>
Copy after login

The output is:

1欢迎访问sina新浪
Copy after login

and above That’s the entire content of this article, I hope it will be helpful to everyone’s study.


Related recommendations:

Detailed explanation of how to intercept the length of string in PHP

PHPStringReverse order implementation method

php implementation to obtain a specified number of random string method

The above is the detailed content of PHP implements the solution to intercept Chinese strings without the ? sign_php tips. For more information, please follow other related articles on the PHP Chinese website!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template