I have a text variable which contains multiple images with a relative or absolute path. I need to check if the src attribute starts with http or https then ignore it, but in case it starts with / or something like abc/ then prepend a base url. I tried like below: My output is: Issue here: I got 99% result
Tag: replace
Find and Replace Multi-Row Phrases/text Within PHP Arrays without damaging the array
Lets say I have an array: Keeping the structure of this array intact, I want to be able to replace phrases within it, even if they spill over to the next or previous string. Also I don’t want punctuation or case to impact it. Examples: String to Find: “Sundays which are also pretty good” Replace with: “Mondays which are also
Replacing part of string with prefix and suffix
I want to make it Texts to replace with is with in braces () or [] and can be found from Table 1 or Table 2. Current approach How to improve the code to skip not-required loop ? Answer There may be a way with one loop that I’ll think on, but since the arrays have the same keys they
Php str_replace one by one in loop
I have a string like A name Tokyo 26, B name Moscov 45, C name Newyork 26, D name Berlin 67, E name Paris 37, F name London 39 I want to replace name words as name1 name2 name3…. so the final string …
Remove blacklisted terms from string then eliminate unnecessary spaces
I have an array of blacklisted terms: and I have a string to sanitize: I just want to remove the $arrayBlacklist value if it’s in the $city variable. So, I get $city = “Jakarta Selatan” Answer Sort the array based on string length to avoid overlapping issues using usort. preg_replace each of the string in a case-insensitive manner. Finally, remove
How to remove empty CSS property by PHP?
I have a string like this .header{ font-family:; background-color:red; color:blue; line-height:; } .footer{ background-color:green; color:; } I want to convert this string-like this .header{ …
Replace column values in result set using a lookup/dictionary array
You can find the entire code below. I have 2 arrays. The first array looks like this: The second array is this: (from a mysqli result set) I want to replace the logMessage values in the second array with the corresponding $replacements value. How it should look: Must be Current code: Answer Maybe something like the following. Grabs the key
Find known string in file then add string prior to it
I need to find a known string inside a config file and then add some content(another string) prior to this. What is the most efficient way to do this? Example: filetomodify.config Desired outcome: I’ve been using fopen to access the file but this only allows to prepend or append content, by default with fwrite. I can’t currently think of an
Parse and reformat text with optionally occurring trailing characters
Due to horrible and inconsistent formatting by a certain website we are receiving data from, I need to parse the following string and print new strings after replacing/removing one or two substrings. Desired result: How can I parse a line with two ‘-‘ into the corresponding Owner: name format? My current code: Answer This will definitely fail in the future,
PHP: How can I remove from String the last word?
How can I remove, with PHP, the last word from a String? For example the string “Hi, I’m Gian Marco” would become “Hi, I’m Gian”.