Skip to content

Tag: string

php: the best way to check if string ends with an element from array?

What’s the best way to check if $str1 ends with some string from $arr1? The answer true/false is great, although knowing the number of $arr1 element as an answer (if true) would be great. Example: Is there any better/faster/special way than just comparing in for-statement all elements of $arr1 with the …

Convert list into an array using php. How?

I have a function called listelements(), which outputs text like <li>text1</li><li>text2</li>. I have more than 500 elements. Now I want to convert it into an array. Can anyone help me? Thanks. Note: I’m using php Update: The thing i want to achieve is alphabetical navigation. As…

Obtain first line of a string in PHP

In PHP 5.3 there is a nice function that seems to do what I want: Unfortunately, the server runs PHP 5.2.17 and the optional third parameter of strstr is not available. Is there a way to achieve this in previous versions in one line? Answer For the relatively short texts, where lines could be delimited by eit…

PHP: delete the first line of a text and return the rest

What’s the best way to remove the very first line of a text string and then echo the rest in PHP? For example. This is the text string: This is the final output: If I was working with a file in Bash I could do easily the next: sed -i~ 1d target-file Or: tail -n +2 source-file > target-file Any

how to check if PHP variable contains non-numbers?

I just want to know the method to check a PHP variable for any non-numbers and if it also detects spaces between characters? Need to make sure nothing weird gets put into my form fields. Thanks in advance. Answer If you mean that you only want a value to contain digits then you can use ctype_digit().