Find complete strings in data based on portions of a string

Question

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);

Show source
| php   | search   | strip   2017-01-03 15:01 1 Answers

Answers to Find complete strings in data based on portions of a string ( 1 )

  1. 2017-01-03 16:01
    <?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
    )
    

Leave a reply to - Find complete strings in data based on portions of a string

◀ Go back