PHP is a widely used server-side programming language. It can query databases such as MySQL and Oracle. In practical applications, we often need to query and analyze text. This article will introduce how to use PHP to query text.
PHP provides many functions to process text, including string functions and regular expressions. We can use these functions to search and replace text.
For example, we can use the strpos function to find substrings in text:
<?php $str = "Hello world!"; $pos = strpos($str, "world"); if ($pos === false) { echo "未找到。"; } else { echo "字符串“world”在字符串“Hello world!”中的位置为:$pos"; } ?>
Output results:
字符串“world”在字符串“Hello world!”中的位置为:6
In addition, we can also use the preg_match function to use regular expressions Formula for text matching:
<?php $str = "Hello world!"; $pattern = "/world/"; if (preg_match($pattern, $str)) { echo "字符串“world”在字符串“Hello world!”中找到了。"; } else { echo "未找到。"; } ?>
Output results:
字符串“world”在字符串“Hello world!”中找到了。
Commonly used query statements include SELECT, INSERT, UPDATE and DELETE. Here we mainly explain the SELECT statement, which can be used to retrieve data from the database.
For example, we can use the following PHP code to query all records in a table named "users":
<?php //连接数据库 $conn = mysqli_connect("localhost", "root", "", "mydb"); //检查连接是否成功 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } //查询数据 $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); //输出数据 if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { echo "ID: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>"; } } else { echo "0 结果"; } //关闭连接 mysqli_close($conn); ?>
This code will output all records in the table named "users" Records, including each record's ID, name, and email.
We can also use PHP to query the text on the site. Here, we can use the file_get_contents function to get the content of the web page, and then use string functions and regular expressions for text processing.
For example, the following code will get the HTML document from the URL "https://www.example.com/" and find all the links:
<?php $html = file_get_contents("https://www.example.com/"); $pattern = '/<a\s+.*?href=[\"\']?([^\"\'>]+)[\"\']?.*?>/i'; preg_match_all($pattern, $html, $matches); foreach ($matches[1] as $url) { echo $url . "<br>"; } ?>
This code will output the found All links.
In addition to querying and searching text, PHP also provides many functions to process text. For example, we can use the following code to convert a string to uppercase:
<?php $str = "Hello World!"; echo strtoupper($str); ?>
Output result:
HELLO WORLD!
In addition, we can also use the urlencode function and urldecode function to perform URL encoding and decoding.
In short, PHP is a very flexible and powerful programming language that can be used to process all kinds of text. Whether you're working with text in a database, querying text on a site, or doing text processing, PHP can handle it with ease.
The above is the detailed content of Let's talk about how to use PHP to query text. For more information, please follow other related articles on the PHP Chinese website!