Making all occurrences of a specific word capital? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a few recurring words on my website that appear quite frequently. Is there any way to make all occurrences of that specific word all capitals?

You can use str_ireplace. It's the case-insensitive version of str_replace. Once you have the content from the database inside of a variable you can do the following before you output it:
$content = str_ireplace('replace', 'REPLACE', $content);
This is searching for the case-insensitive text replace inside of $content and changing all occurrences to REPLACE. This will not only change whole words, it will change partials as well, for example it will change replaced to REPLACEd.
If you would like to only select whole words you will need to use regular expressions with the preg_replace function. Example:
$content = preg_replace('/(\W|^)replace(\W|$)/i', '${1}REPLACE${2}', $content);
This matches all case-insensitive occurrences of the word replace that are either next to a non-word character (/W) or the beginning (^) or end ($) of a line, and replaces it with REPLACE plus whatever was before it (${1}) and after it (${2}).

Related

Why is the Regex for my product's code not working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to do Regex code for product's code in VSCode's HTML. My product's code has the following conditions:
Required enter 6 characters
First 2 characters must be letter and uppercase
Next 4 characters must be numbers.
I have tried this regular expression and it doesn't work:
^[A-Z]{2}+\[0-9]{4}$
Your regex should be:
^[A-Z]{2}[0-9]{4}$
This corrects the escaping of your character class; that made it no longer a character class but a series of characters to match in the regex, ending with 4 ]s. The + also is not needed as the {2} is stating only 2 uppercase alpha characters are allowed.
You can also swap the [0-9] with \d which is the metacharacter for an integer. With PHP regexs you also need delimiters so something like:
/^[A-Z]{2}\d{4}$/
could be used in preg_match.

PHP Place each word in a paragraph in a <span> without punctuation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
How do I place each word in a paragraph in its individual span tag? Currently I am using explode() and using a space to do this, but the I get the following:
"<span>Hello,</span> <span>I</span> <span>am</span> <span>Alec.</span>"
I will be using a dictionary lookup for each word, but "Hello," will not find what I'm looking for. Furthermore, I am stylizing the span depending on the word selected (through an onClick), so I don't want the comma to be part of the span.
Eventually, I want the following code to populate:
"<span>Hello</span>, <span>I</span> <span>am</span> <span>Alec</span>."
Theoretically it would be nice to do this without using explode() since I have to add all of these characters back into the text, but outside of the <span>. Is there a function to accomplish this?
Thank you!
Edit: I will be using this for everyday writing in different languages, so hyphenated words or letters outside of "A-Za-z" are expected.
preg_replace() can do that:
$str = 'Hello, I am Alec.';
echo preg_replace('/(\w+)/', '<span>${1}</span>', $str);
Output:
<span>Hello</span>, <span>I</span> <span>am</span> <span>Alec</span>.

Regex to match PHP Function definition [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need a regex string to match PHP Function definitions like:
function anyname(any, number, of, params) {
in editors like Geany or Notepad++ and insert a static text (ofcourse PHP Code) such as var_dump(debug_backtrace()); inside the matched function definitions.
As I have to debug a very large PDF Class file of size around 1.24MB and having more than 60 function definitions.
Can anyone help ?
How about this:
function\s+.*?\)\s*{
I did a rough test and it works on my Notepad++.
EDIT
If you need to replace these with the debug backtrace, do this:
Find:
function\s+(.*?)\)\s*{
Replace:
function \1\) {\nvar_dump\(debug_backtrace\(\)\);
Tested with Notepad++.
Explanation:
The regex itself is nothing too fancy. The .*? simply means "Match anything, ungreedy". It will match everything until it sees a closing bracket and stop.
During the find and replacement, the .*? is enclosed into a bracket, because we need to take this out as the texts we need to preserve, and then fill it up in the \1 position in the replacement.

How to format numbers using Regular Expression and PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am practicing for regular expressions.
I was trying to format numbers using PHP and regex. I want to add comma after each 3 digits like this 111222333444 to this format 111,222,333,444 or 11222333444 to 11,222,333,444 by using PHP and Regular expression.
I searched a lot but I could not find exact solution for this.
I know that there is function in php (number_format) to do this but I want to use Regular expression and PHP to do this because I am learning regex and practicing so I want to use regex and php only.
Here is a regex based solution:
$repl = preg_replace('/(?!^)(?=(?:\d{3})+$)/m', ',', $input);
RegEx Demo
Explanation:
(?!^) - Negative lookahead to make sure we are not at start of input
(?=(?:\d{3})+$) - Positive lookahead to make sure there 1 or more of 3 digit sets following current position
Replacement is just a literal comma
More explanation is available at linked demo

Regular expression for special characters and numericals [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Want a regular expression that matches only special characters and numericals..
Consider an example
$sting1 = '($001)';
$sting2 = '($001test)';
So only $string1 should match in this case and not the second one.
As second string has alphabets present in it, that should not match..
How about:
preg_match('/^\P{L}+$/u', $str);
Where \P{L} stands for any character that is not a letter.
Below is a PCRE regex which can match special characters, numericals and white space.
[^[:alpha:]]+
If you need to omit whitespaces then,
[^[:alpha:]\s]+
You can check out the demo here.

Categories