Php lấy văn bản từ url

Gần đây, tôi đang phát triển một giải pháp tùy chỉnh cho một trong các dự án của khách hàng của mình và tôi đã gặp sự cố. Một tin nhắn [chuỗi] đã được lưu trữ trong cơ sở dữ liệu có thể chứa các url. Tôi phải hiển thị thông báo đó và nếu nó chứa các url – nó phải được hiển thị dưới dạng các liên kết có thể nhấp được và đang hoạt động. Vì vậy, đây là những gì làm việc cho tôi

Đoạn mã PHP. Tìm URL trong chuỗi và tạo liên kết bằng hàm

$yourTextWithLinks = 'Here is an example for a text [string] that contains one or more url. Just visit //www.google.com/ or //google.com and this is the end of the example.';
$text = strip_tags[$yourTextWithLinks];

function displayTextWithLinks[$s] {
  return preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $s];
}

$text = displayTextWithLinks[$text];
echo $text;

Thông thường, nếu bạn lặp lại biến $yourTextWithLinks, nó sẽ hiển thị văn bản thuần túy không có liên kết hoạt động. Vì vậy, bây giờ bạn có thể chuyển chuỗi của mình tới hàm displayTextWithLinks[] và sẽ trả về một văn bản có các liên kết có thể nhấp được trong đó

Nếu bạn muốn sử dụng nó mà không có chức năng, đây là cách

Đoạn mã PHP. Tìm URL trong văn bản và tạo liên kết mà không cần chức năng

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;

Học nhiều hơn về

  • preg_replace
  • dải_tags

Độc giả của chúng tôi rằng giải pháp trên không hoạt động với các url chứa các ký tự không phải là chữ Latinh và đã tìm thấy thư viện sau

đánh dấu url

Đánh dấu url là một thư viện PHP để phân tích cú pháp URL từ đầu vào chuỗi. Hoạt động với các URL phức tạp, trường hợp cạnh và đầu vào được mã hóa

Các tham số từ một chuỗi URL có thể được truy xuất trong PHP bằng cách sử dụng các hàm parse_url[] và parse_str[]

Ghi chú. URL trang và các tham số được phân tách bằng dấu ?

hàm parse_url[]. Hàm parse_url[] được sử dụng để trả về các thành phần của URL bằng cách phân tích cú pháp URL đó. Nó phân tích cú pháp một URL và trả về một mảng kết hợp chứa các thành phần khác nhau của nó

cú pháp.  

parse_url[ $url, $component = -1 ]

hàm parse_str[]. Hàm parse_str[] được sử dụng để phân tích một chuỗi truy vấn thành các biến. Chuỗi được chuyển đến chức năng này để phân tích cú pháp có định dạng chuỗi truy vấn được chuyển qua URL

cú pháp.   

parse_str[ $string, $array ]

Cách tiếp cận. Phân tích cú pháp chuỗi URL bằng cách sử dụng hàm parse_url[] sẽ trả về một mảng kết hợp có chứa các thành phần khác nhau [URL đã truyền] của nó. Truy vấn của mảng được trả về bởi hàm parse_url[] chứa chuỗi truy vấn URL

Các ví dụ dưới đây sử dụng hàm parse_url[] và parse_str[] để lấy các tham số từ chuỗi URL.  

ví dụ 1.   

PHP




parse_str[ $string, $array ]
5

 

parse_str[ $string, $array ]
6

parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
8
parse_str[ $string, $array ]
9
parse_str[ $string, $array ]
0

parse_str[ $string, $array ]
0

parse_str[ $string, $array ]
1

parse_str[ $string, $array ]
2

parse_str[ $string, $array ]
3

parse_str[ $string, $array ]
4
parse_str[ $string, $array ]
8
parse_str[ $string, $array ]
6
parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
9

 

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
20

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
21

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
22
parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
4
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
25
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
26
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
27
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
28
parse_str[ $string, $array ]
9

parse_str[ $string, $array ]
0

parse_url[ $url, $component = -1 ]
31

parse_url[ $url, $component = -1 ]
32
parse_url[ $url, $component = -1 ]
33
parse_url[ $url, $component = -1 ]
34
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
28
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
25
parse_url[ $url, $component = -1 ]
37
parse_url[ $url, $component = -1 ]
38

 

parse_url[ $url, $component = -1 ]
39

đầu ra.

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
2

 

ví dụ 2.  

PHP




parse_str[ $string, $array ]
5

 

parse_str[ $string, $array ]
6

parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
8
parse_str[ $string, $array ]
54
parse_str[ $string, $array ]
0

parse_str[ $string, $array ]
0

parse_str[ $string, $array ]
1

parse_str[ $string, $array ]
2

parse_str[ $string, $array ]
3

parse_str[ $string, $array ]
4
parse_str[ $string, $array ]
8
parse_str[ $string, $array ]
6
parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
9

 

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
20

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
21

$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
22
parse_str[ $string, $array ]
7
parse_str[ $string, $array ]
4
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
25
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
26
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
27
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
28
parse_str[ $string, $array ]
9

parse_str[ $string, $array ]
0

parse_url[ $url, $component = -1 ]
31

parse_url[ $url, $component = -1 ]
32
parse_url[ $url, $component = -1 ]
33
parse_url[ $url, $component = -1 ]
34
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
28
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
25
parse_url[ $url, $component = -1 ]
37
parse_str[ $string, $array ]
84
parse_str[ $string, $array ]
85
parse_url[ $url, $component = -1 ]
34
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
28
$text = strip_tags[$yourTextWithLinks];
$textWithLinks = preg_replace['@[https?://[[-\w\.]+[-\w]]+[:\d+]?[/[[\w/_\.#-]*[\?\S+]?[^\.\s]]?]?]@', '$1', $text];
echo $textWithLinks;
25
parse_str[ $string, $array ]
89
parse_url[ $url, $component = -1 ]
38

 

parse_url[ $url, $component = -1 ]
39

đầu ra.

parse_url[ $url, $component = -1 ]
3

 

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này

Chủ Đề