How can URLs be extracted from a text and stored in an array in PHP?
To extract URLs from a text and store them in an array in PHP, you can use regular expressions to match the URLs in the text and then store them in an array. This can be useful for tasks like parsing text for links or extracting specific information from a text body.
<?php
$text = "Check out my website at https://www.example.com. Also, visit our blog at http://blog.example.com.";
$pattern = '/(https?:\/\/[^\s]+)/';
preg_match_all($pattern, $text, $matches);
$urls = $matches[0];
print_r($urls);
?>
Related Questions
- What is the best way to insert smileys into a guestbook form using Java and display them when writing input texts to a text file?
- How can one determine the appropriate licensing model for a PHP-based browser game to balance between copyright protection and player access?
- What are some potential pitfalls to be aware of when handling multiple entries in PHP and databases?