Plug-in Posted August 30, 2011 Share Posted August 30, 2011 I'm trying to make a XML api word blacklist and I'm able to get all the information fine but I'm confusing myself with the array, could you please help, this is my code: <pre> <?php $data = file_get_contents("http://api.jspace.netai.net/blacklist.xml"); $data = simplexml_load_string($data); //foreach ($data->words->word as $word) { //echo (string)$word; //} function filter_words($text, $data) { foreach($data->words->word as $w) { $string = str_replace((string)$w, '[!]', strtolower($text)); return strtoupper($string); } } echo filter_words("Sentence containing unfiltered word - shit.", $data); ?> </pre> The problem is it returns unfiltered Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/ Share on other sites More sharing options...
trq Posted August 30, 2011 Share Posted August 30, 2011 return exits a function when it is executed, so your loop will only execute once, then exit. Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263431 Share on other sites More sharing options...
Plug-in Posted August 30, 2011 Author Share Posted August 30, 2011 How could I do this alternatively then, I'm quite confused :/ Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263435 Share on other sites More sharing options...
silkfire Posted August 30, 2011 Share Posted August 30, 2011 Put return after the loop? Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263439 Share on other sites More sharing options...
trq Posted August 30, 2011 Share Posted August 30, 2011 Put return after the loop. Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263442 Share on other sites More sharing options...
Plug-in Posted August 30, 2011 Author Share Posted August 30, 2011 I'm sorry if I look like I want to be spoon fed but I cannot understand why this will not work, the updated code is below and still does not return [!], sorry for the cuss words: <pre> <?php $data = file_get_contents("http://api.jspace.netai.net/blacklist.xml"); $data = simplexml_load_string($data); function filter_words($text, $data) { foreach($data->words->word as $w) { $string = str_replace(strtolower((string)$w), '[!]', strtolower($text)); } return strtoupper($string); } echo filter_words("fuck", $data); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263449 Share on other sites More sharing options...
trq Posted August 30, 2011 Share Posted August 30, 2011 function filter_words($text, $data) { foreach($data->words->word as $w) { $text = str_replace(strtolower($w), '[!]', strtolower($text)); } return strtoupper($text); } Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263479 Share on other sites More sharing options...
Plug-in Posted August 30, 2011 Author Share Posted August 30, 2011 You bute, I'm going to have to read up on (string), obviously my usage of it was incorrect. Thanks a lot for you help. Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263518 Share on other sites More sharing options...
trq Posted August 31, 2011 Share Posted August 31, 2011 That wasn't necessary and also not the issue. Quote Link to comment https://forums.phpfreaks.com/topic/246015-array-help/#findComment-1263842 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.