1

I want to know if it is possible to check if a textbox contains any link. And if it is possible to select that link out of the field.

I tried it like this, but the problem is that i can't select the whole link and the query is very imprecise.

$a = 'Lorem ipsum dolor sit amet, consetetur sadipscing www.loremipsum.com/asd/fgh/jkl elitr, sed diam ';

                if (strpos($a, 'www.') !== false) {
                    echo 'true';
                }

So again for explanation:

I have a text field which can contain a comment but also links. I need to select the correct link out of this field and safe it seperatly in a variable.

I don't want you to code my code but it would be great if anyone know a function or a method to realize this.

Thank you!

Jannik Window
  • 43
  • 2
  • 10
  • 1
    https://stackoverflow.com/questions/910912/extract-urls-from-text-in-php – Jurij Jazdanov Aug 31 '17 at 13:24
  • Try using a regular expression like the top voted answer in https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url along with `preg_match` or `preg_match_all` to retrieve the URL. – Matt Rink Aug 31 '17 at 13:24
  • Possible duplicate of [Extract URLs from text in PHP](https://stackoverflow.com/questions/910912/extract-urls-from-text-in-php) – JustBaron Aug 31 '17 at 13:25

2 Answers2

2

Here's a regex that matches www.whatever https://regex101.com/r/rdOKQB/1/

<?php

$string = 'Lorem ipsum dolor sit amet, consetetur sadipscing www.loremipsum.com/asd/fgh/jkl elitr, sed diam ';
$regex= '#www\.\S+#';

preg_match_all($regex,$string, $matches);
var_dump($matches);

Which outputs : www.loremipsum.com/asd/fgh/jkl See it here: https://3v4l.org/4t2oV

You might want another regex testing for http:// or https:// instead/too? https://regex101.com/r/rdOKQB/2

#https?:\/\/\S+#
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
0

Instead of strpos you can use strstr which returns next word.