1

The code:

    $row['text'] = 'http://t.co/iBSiZZD4 and http://t.co/1rG3oNmc and http://t.co/HGFjwqHI and http://t.co/8UldEAVt';

    if(preg_match_all('|http:\/\/t.co\/.{1,8}|i',$row['text'],$matches)){
        foreach($matches[0] as $value){
            $headers = get_headers($value,1);
            if(is_array($headers['Location'])){
                $headers['Location'] = $headers['Location'][0];
            }
            $row['text'] = preg_replace('|http:\/\/t.co\/.{1,8}|i', '<a href="' . $headers['Location'] . '">' . $headers['Location'] . '</a>',$row['text']);
        }
    }

This is related to get_headers(). Sometimes get_headers($url,1) returns an array with a location index key like so: [Location]=>Array([0]=>url1 [1]=>url2). I basically want to make [Location] equal to [Location][0] if [Location][0] exists. However, the above code doesn't seem to accomplish that task. I've also tried array_key_exists() and isset() but neither solved the problem. Thoughts?

6
  • have you made sure $headers['Location'] is an array? Commented Aug 4, 2012 at 20:07
  • $headers['Location'] is not normally an array. That's why I'm checking if it is an array so I can take alternative action. Commented Aug 4, 2012 at 20:11
  • this should work... Make sure that $headers is not read-only. Commented Aug 4, 2012 at 20:13
  • 1
    @cwscribner: What do you mean it doesn't work? What is the result you're getting? Commented Aug 4, 2012 at 20:16
  • Your code works fine for me, make sure your array looks like what you think it looks like. codepad.viper-7.com/DKjPM1 Commented Aug 4, 2012 at 20:25

1 Answer 1

2

Don't try to replace on the fly. First get all the values, and then do the replace in one batch (using two arrays as the $search and $replace parameters).

<?php

    $replace = array();
    $row = array('text' => 'http://t.co/iBSiZZD4 and http://t.co/1rG3oNmc and http://t.co/HGFjwqHI and http://t.co/8UldEAVt');
    if (preg_match_all('|http:\/\/t.co\/.{1,8}|i', $row['text'], $search)) {
        foreach ($search[0] as $value) {
            $headers = get_headers($value, 1);
            if (is_array($headers['Location'])) {
                $headers['Location'] = $headers['Location'][0];
            }
            $replace[] = "<a href='{$headers["Location"]}'>{$headers["Location"]}</a>";
        }
        $row['text'] = str_replace($search[0], $replace, $row['text']);
        echo $row["text"];
    }

P.S. - Next time, please tell us the context of your problem, tell us you "are making a service that resolves shortened URLs", don't let me figure that out from your code alone.

Sign up to request clarification or add additional context in comments.

1 Comment

On a side note, I think I may have to find an alternative to get_headers(). It keeps failing to open stream, maybe because of the amount of links I have.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.