Asked  7 Months ago    Answers:  5   Viewed   30 times

Trying to replace all occurrences of an @mention with an anchor tag, so far I have:

$comment = preg_replace('/@([^@ ])? /', '<a href="/$1">@$1</a> ', $comment);

Take the following sample string:

"@name kdfjd fkjd as@name @ lkjlkj @name"

Everything matches okay so far, but I want to ignore that single "@" symbol. I've tried using "+" and "{2,}" after the "[^@ ]" which I thought would enforce a minimum amount of matches, but it's not working.



Replace the question mark (?) quantifier ("optional") and add in a + ("one or more") after your character class:

@([^@ ]+)
Wednesday, March 31, 2021
answered 7 Months ago

You need to specify a matching group using brackets in order to extract content.

preg_match("//([^/]+)$/", "5020203010101/FIS/CASH FUND/SBG091241212", $matches);

echo $matches[1];
Wednesday, March 31, 2021
answered 7 Months ago

I would not recommend using regular expressions for a task like this.
A simple function to check a string for properly written brackets is quickly written:

* @param $str input string
* @returns boolean true if all brackets are properly opened and closed, false otherwise
function checkBraces($str)
    $strlen = strlen($str); // cache string length for performance
    $openbraces = 0;

    for ($i = 0; $i < $strlen; $i++)
        $c = $str[$i];
        if ($c == '<') // count opening bracket
        if ($c == '>') // count closing bracket

        if ($openbraces < 0) // check for unopened closing brackets
            return false;

    return $openbraces == 0; // check for unclosed open brackets

Using this code as a basis, it shouldn't be too hard to implement a check to verify whether or not the tag name of opening and closing brackets also matches - but I'll leave that to you :-)

Wednesday, March 31, 2021
answered 7 Months ago

Use !==FALSE instead of !=FALSE. The problem is that stripos returns 0 if the needle is at the start of the haystack, and 0 is falsy. By using !== you are forcing it to ensure the result is actually false, and not just 0.

This is actually listed in the docs. An "RTM" might be appropriate here.

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Saturday, May 29, 2021
answered 5 Months ago

This should do it

old_string = "this is going to have a full stop. some written sstuff!"
k = old_string.rfind(".")
new_string = old_string[:k] + ". - " + old_string[k+1:]
Saturday, September 4, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :