Find complete strings in data based on portions of a string

31 views Asked by At

I have a data set (plain text) where I want to find certain links. The links I want to find always start with the same portion of string. How can I create an array with all the results?

EXAMPLE

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec aliquet, nulla id vestibulum efficitur, link1.com/just/a/link/1 diam ligula pharetra libero, sed pulvinar link1.com/just/a/link/2 massa ante nec justo. Phasellus tristique dignissim justo sed luctus. Duis link2.com/just/a/link/1 vel scelerisque est. Vestibulum sit amet leo lacinia, link1.com/just/a/link/2 iaculis mi ut, ullamcorper libero.

In this piece I have two portions of a certain string (link1.com/ and link2.com/) and after that we have 2 links to strip.

Any idea how I can get all four links in just one array?

I tried something like this:

$data = str_replace('"', "", $data);
$data = str_replace("'", "", $data);
$data = str_replace("’", "", $data);
$data = str_replace("…", "", $data);
$data = strtolower($data);
$data = trim(preg_replace('/\s+/', ' ', $data));
$delimiter = "link1.com/just/a/link/";
$array = explode($delimiter, $data);
1

There are 1 answers

2
Abhishek On
<?php
$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec aliquet, nulla id vestibulum efficitur, link1.com/just/a/link/1 diam ligula pharetra libero, sed pulvinar link1.com/just/a/link/2 massa ante nec justo. Phasellus tristique dignissim justo sed luctus. Duis link2.com/just/a/link/1 vel scelerisque est. Vestibulum sit amet leo lacinia, link1.com/just/a/link/2 iaculis mi ut, ullamcorper libero.";

$arr = explode(" ",$str);
$link = array();


foreach($arr as $word)
{
    if(strpos($word,".com"))
    {
        array_push($link,$word);
    }
}

print_r($link);

?>

OUTPUT

Array
(
    [0] => link1.com/just/a/link/1
    [1] => link1.com/just/a/link/2
    [2] => link2.com/just/a/link/1
    [3] => link1.com/just/a/link/2
)