PHP stripos in LUA

1

I want to convert this PHP code into LUA language. The problem is that there is no stripos function that counts the first occurrence of the desired LUA word.

How can I convert the code below?

<?php
  echo stripos("I love php, I love php too!","PHP");
?>
    
asked by anonymous 06.05.2015 / 18:37

1 answer

4

In order to do the case-insensitive search, you can use string.lower() to convert the letters to lowercase and look for the first occurrence with string.find() .

function stripos (palheiro, agulha)
    palheiro = string.lower(palheiro)
    agulha = string.lower(agulha)

    if palheiro ~= nil and agulha ~= nil then   
        return (string.find(palheiro, agulha) -1)   
    else  
        return nil   
    end   
end

print(stripos("I love php, I love php too!", "PHP"))  

DEMO

    
06.05.2015 / 19:22