Skip to content
Advertisement

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 end of $str1? Answer Off the top of my

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 of now my function displays links in list order. Instead of that i want

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 either one (“n”) or

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

Split string on non-alphanumeric characters and on positions between digits and non-digits

I’m trying to split a string by non-alphanumeric delimiting characters AND between alternations of digits and non-digits. The end result should be a flat array of consisting of alphabetic strings and numeric strings. I’m working in PHP, and would like to use REGEX. Examples: ES-3810/24MX should become [‘ES’, ‘3810’, ’24’, ‘MX’] CISCO1538M should become [‘CISCO’ , ‘1538’, ‘M’] The input

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().

Advertisement