Search code examples
phprssstr-replacefeed

PHP : str_replace in PHP with removing extra symbols in string


Currently I want to pass links to my RSS feed parser from my postgreSQL database.

So far I was able to convert to an array of a single string the string reads like:

{"(\"(http://www.delawareriverkeeper.org/rss.xml)\")","(\"(http://www.littoralsociety.org/index.php?format=feed&type=rss)\")","(\"(\"\"http://www.nj.gov/dep/newsrel/newsrel.rss \"\")\")"}

Only one string.

We want to remove the extra parantheses, quotes, backslashes and brackets so it reads like "http://www.delawareriverkeeper.org/rss.xml", "http://www.littoralsociety.org/index.php?format=feed&type=rss" and so on.

We then plan splitting that into another array to pass into our parser.

I think str_replace is what I am looking for but I am having trouble of actually figuring out what to use to remove the extra quotes.


Solution

  • The following regex matches any url into an array, so there's no need to "splitting that into another array to pass into our parser"

    <?php
    $subject= '{"(\"(http://www.delawareriverkeeper.org/rss.xml)\")","(\"(http://www.littoralsociety.org/index.php?format=feed&type=rss)\")","(\"(\"\"http://www.nj.gov/dep/newsrel/newsrel.rss \"\")\")"}';  
    preg_match_all('/\b(?:(?:https?):\/\/|www\.)[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[-A-Z0-9+&@#\/%=~_|$]/ix', $subject, $result, PREG_PATTERN_ORDER);
    print_r($result[0])
    

    OUTPUT:

    Array
    (
        [0] => http://www.delawareriverkeeper.org/rss.xml
        [1] => http://www.littoralsociety.org/index.php?format=feed&type=rss
        [2] => http://www.nj.gov/dep/newsrel/newsrel.rss
    )
    

    Ideone Demo