1

I'm trying to find all things inside double quotes and replace it with a link using it. I have over 500 lines of questions, so I don't want to do it by hand.

Original php doc snippet:

$q2 = array ("What does Mars look like from Earth?",
"What is Mars's position relative to Earth?");

$q3 = array ("What does Mars's surface look like?",
"Show me a view of the surface of Mars.",
"Show me a picture of the surface of Mars.");

Formatting I want:

$q2 = array ("<a href="answer.php?query=What+does+Mars+look+like+from+Earth%3F">What does Mars look like from Earth?</a>",
<a href="answer.php?query=What+is+Mars's+position+relative+to+Earth%3F">"What is Mars's position relative to Earth?");

I tried using Regex, but without any previous experience with it, I was unsuccessful. Using RegExr (my example) I came up with a find of: "[A-Za-z0-9\s.\?']*" and a replace of: < a href=answer.php?query=$&>$&"

This just gave results like

$q2 = array (<a href=answer.php?query="What does Mars look like from Earth?">"What does Mars look like from Earth?"</a>",

This is close, but not what I need. Hopefully someone knows what replace I should use, or a better program to try. Any help would be appreciated.

5
  • 1
    Why not just build the arrays you need dynamically from the arrays you have? It would be much easier to update your array values if you don't need to worry about writing the HTML, properly URL-encoding the string, etc. Just write a function by which you can pass in your array of questions and receive an array of links. By the way, you should have quotes around your href values is you want properly formatted HTML. Commented Aug 19, 2013 at 16:36
  • Maybe you can try split using space as delimiter? And put each split array back to a string. Commented Aug 19, 2013 at 16:37
  • I don't know what that means, but I'm up for any suggestion. I don't need to use Regex, it was just an idea. Commented Aug 19, 2013 at 16:38
  • @Mike How would I write a function that does that? Thanks Commented Aug 19, 2013 at 16:42
  • I do not think this is possible in a single regex. Regex will only match what is actually present. It's ability to "transform" stops at a very simple replace function. You've already found your search text, but you'd need a second regex to find the spaces in your search text and replace them with plusses. Commented Aug 19, 2013 at 16:44

3 Answers 3

1

Why not just make a function like this to which you can pass your array and get an array of links returned?

function make_questions_into_links($array) {
    if (!is_array($array)) {
        throw new Exception('You did not pass an array')
    } else if (empty($array)) {
        throw new Exception('You passed an empty array');
    }

    return array_map(function($element) {
        return '<a href="answer.php?query=' . urlencode($element) . '">' . $element . '</a>';
    }, $array);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I would run them though a function like below. instead of updating your source code with a regex.

function updateQuestions(&$questions){
    foreach($questions as $key => $value){
        $questions[$key] = '<a href="answer.php?query=' . urlencode($value) . '">' . $value . '</a>';
    }
}

updateQuestions($q2);

2 Comments

Let me try that and I'll get back to you guys on if it works. Thanks
I'm getting a syntax error on line two. (I tried putting it in a php doc and running it through)
0

Following code should work:

$q2 = array ('"What does Mars look like from Earth?"',
             '"What is Mars\'s position relative to Earth?"'
            );
$aq2 = preg_replace_callback(array_fill(0, count($q2), '/(?<!href=)"([^"]+)"/'),
      function($m){return '<a href="answer.php?query='.urlencode($m[1]).'">'.$m[1].'</a>';},
      $q2);

// test the output
print_r($aq2);

OUTPUT:

Array
(
    [0] => <a href="answer.php?query=What+does+Mars+look+like+from+Earth%3F">What does Mars look like from Earth?</a>
    [1] => <a href="answer.php?query=What+is+Mars%27s+position+relative+to+Earth%3F">What is Mars's position relative to Earth?</a>
)

Comments

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.