0

Maybe its a dublicate but every Situation is different :)

I get a String from Ajax Request and handle it to this function

function convertHashtags($str){

    $text = $str;
    $regex = '/#(\w+)/';
    preg_match_all($regex, $text, $allMatches, PREG_SET_ORDER);
    foreach ($allMatches as $matches) { 
        $engine->checkHashtag($matches[0]);
    }

    $regex2 = "/#+([a-zA-Z0-9_]+)/";
    $str = preg_replace($regex2, '<a href="hashtag.php?tag=$1">$0</a>', $str);
    return($str);
}

Then i want in the for each that for every Result the Engine is inserting it to Database. When i comment out the Engine line then its working. return $matches[0]; is working too!

This is my Engine function code

function checkHashtag($tag) {
    return true;
}

And at least here is my Error Message...

500 (Internal Server Error)send

@ jquery-1.10.2.js:8706jQuery.extend.ajax

@ jquery-1.10.2.js:8136(anonymous function)

@ (index):797jQuery.event.dispatch

@ jquery-1.10.2.js:5095jQuery.event.add.elemData.handle

@ jquery-1.10.2.js:4766

Why is it not working ?

*What is $Engine ? *
A included Class

require_once('../PATH/engine_class.php');
$engine = NEW engine_class();
Maddy S.
  • 97
  • 6
  • 1
    What is $engine, looks like a object but its not instantiated anywhere?? Certainly not within the scope of that function – RiggsFolly Aug 13 '15 at 00:36
  • $engine is a included class :) i will edit my Question – Maddy S. Aug 13 '15 at 00:48
  • Check your server error logs, they will tell you the reason for the 500 error. Chances are it's because `$engine` is not within the scope of your function. – Phil Aug 13 '15 at 01:38

1 Answers1

0

preg_match_all returns an int, holding the number of matches. I note that you don't check to see if there were 0 matches. Your code assumes that there is at least one valid match. What value is returned from the routine?

Bob Dill
  • 1,000
  • 5
  • 13
  • I see no such assumption. If there are no matches, OP's code will just return `$str` unmodified. – Phil Aug 13 '15 at 01:39
  • perhaps I'm missing something important, but this link: http://php.net/manual/en/function.preg-match-all.php has the following note: on Return Values Returns the number of full pattern matches (which might be zero), or FALSE if an error occurred. – Bob Dill Aug 13 '15 at 02:53
  • Yes, that's correct but OP isn't assuming anything about the number of matches. If there are no matches, then `$allMatches` will be an empty array, zero `foreach` iterations will run and `preg_replace` will change nothing. – Phil Aug 13 '15 at 03:14
  • I've run into a few places in php where a variable like $allMatches was not an empty array, but a null and encountered the 500 error as a result of that. I agree with your point, but am unclear as to what php actually returns for that variable when there are no results. – Bob Dill Aug 13 '15 at 03:18
  • An empty array, trust me. The only time it would be different is if there was an error and that's irrelevant in this case. – Phil Aug 13 '15 at 03:23
  • To feed this talking a bit. I got where i echo 1 time my return from my Function the Result -> 1 – Maddy S. Aug 13 '15 at 03:36
  • right. so this is a scoping issue within the foreach, so change it to an anonymous function along the lines of: foreach ($allMatches as $matches) { $engine->checkHashtag($matches[0]); } – Bob Dill Aug 13 '15 at 03:42
  • alright, that didn't take, let's try it again: add an anonymous function along the lines of: foreach ($allMatches as $matches) { function ($_matches) {$engine->checkHashtag($_matches);}($matches[0])} That way the scoping is preserved and the function executed in the foreach correctly executes against the right value. – Bob Dill Aug 13 '15 at 03:53
  • oh, duhh! thanks. It doesn't help to mix javascript with php. The issue is where is "engine" defined. It's not defined within the convertHashtags routine, so it's an unknown variable when convertHashtags encounters it. Add one line to the top of your function, immediately after the first curly brace "{": global $engine; This will enable the convertHashtags to know about the (previously defined) $engine variable. – Bob Dill Aug 13 '15 at 11:34