How to convert php string to url entity: 1. Parse the url through parse_url(); 2. Encode and decode the Chinese of the url; 3. Convert the string into html entities through htmlentities and other methods. .
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
php How to convert string to url entity?
PHP string url parsing and entity conversion:
1. Parse the url through parse_url() and return its components:
<?php $str = 'http://www.zymseo.com/admin?uname=zym&pwd=123456'; echo '<pre class="brush:php;toolbar:false">'; print_r(parse_url($str)); echo '<pre/>'; /* Array ( [scheme] => http [host] => www.zymseo.com [path] => /admin [query] => uname=zym&pwd=123456 ) */ echo parse_url($str,PHP_URL_HOST);//www.zymseo.com echo parse_url($str,PHP_URL_PATH);// /admin ?>
2. URL encoding and decoding function to encode and decode the Chinese of the url:
<?php $str = '赵一鸣个人技术博客'; $a = urlencode($str); echo "<a href='test26.php?h=$a'>点击跳转</a>";//网址的中文部分变成了英文编码 echo urlencode($str);//%D5%D4%D2%BB%C3%F9%B8%F6%C8%CB%BC%BC%CA%F5%B2%A9%BF%CD echo urldecode(urlencode($str));//赵一鸣个人技术博客 ?>
3. Convert the string to html entity:
<?php $str = '<h1>赵一鸣SEO技术博客</h1>'; echo $str;//赵一鸣SEO技术博客 //转换中文 echo htmlentities($str);//<h1>ÕÔÒ»ÃùSEO¼¼Êõ²©¿Í</h1> echo htmlspecialchars($str);//不转换【中文】 echo htmlspecialchars_decode($str);//赵一鸣SEO技术博客 ?>
Recommended learning: " PHP video tutorial》
The above is the detailed content of How to convert php string to url entity. For more information, please follow other related articles on the PHP Chinese website!