Home > Backend Development > PHP Problem > How to convert php string to url entity

How to convert php string to url entity

藏色散人
Release: 2023-03-14 12:40:01
Original
2355 people have browsed it

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. .

How to convert php string to url entity

#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 = &#39;http://www.zymseo.com/admin?uname=zym&pwd=123456&#39;;
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    print_r(parse_url($str));
    echo &#39;<pre/>&#39;;
    /*
        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
?>
Copy after login

2. URL encoding and decoding function to encode and decode the Chinese of the url:

<?php
   $str = &#39;赵一鸣个人技术博客&#39;;
   $a = urlencode($str);
   echo "<a href=&#39;test26.php?h=$a&#39;>点击跳转</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));//赵一鸣个人技术博客
?>
Copy after login

3. Convert the string to html entity:

<?php
     $str = &#39;<h1>赵一鸣SEO技术博客</h1>&#39;;
     echo $str;//赵一鸣SEO技术博客
     //转换中文
     echo htmlentities($str);//<h1>ÕÔÒ»ÃùSEO¼¼Êõ²©¿Í</h1>
     echo htmlspecialchars($str);//不转换【中文】
     echo htmlspecialchars_decode($str);//赵一鸣SEO技术博客
?>
Copy after login

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!

Related labels:
php
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