I've been trying for a while now to do this, but I can't quite get it right.
How can I write a function that has an array of a couple of words and then use preg_match to see if a variable starts with one of the words in the array?
This isn't right, but it might give an idea of what I mean.
PHP Code:
function prefix($prefix) $pre = array('the', 'or'); $pre2 = '/^[$pre]/'; if preg_match($pre2, $prefix) {
first, do not use a character class, that would search for the occurrences of the letters, regardless of their order.
second, a set of matching words/search terms is separated by the PCRE OR operator (|).
I am still playing around with my booktitle function just seeing what I can make happen and what I can do to improve upon it to help me learn more.
Right now my function checks for the words 'the' and 'a' at the start of string, if either word is present then they are replaced with an empty string. The thing is that I don't want to type in the word 'thesis' and then simply have 'sis' displayed. So what I've done is added a space behind 'the' and 'a' so if the string is something like 'The red dog jumps" then 'the ' is taken away and only 'red dog jumps' is displayed, but if the string is something like 'Theodore Roosevelt was president' then it would display 'Theodore Roosevelt was president'.
but since the if() part has a return statement (and you can’t execute code after you reached a return), the explicit else is not necessary.
Pretty neat, never knew that. I simply assumed since he renamed and tweaked some details he simply left the else out as well to emphasize the main part he was helping me with.
Dorm's conversion is slightly incorrect, but it does give the correct idea. It would be fine in this block since there is no other evaluation possible.
Would be the actual evaluation for it. The only difference is that with the conversion Dorm gave that second return would only function if the preg_match was false, whilst the actual code would return regardless of any other branch evaluations. Since this only have one branch, than it works just fine. Implicitly, it is an else for a single if branch.
The reason why is braces are NOT required in PHP, but only apply to the next instruction. I personally recommend not using this approach as it does create the potential for a modification blunder. It is valid in the language though, so to each their own.
Personally, I return once and only once from a function, and use logic to reassign. It cost me more in stack memory, but IMO it makes debugging slightly easier.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php