Find Price/Value in PHP string [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to use PHP to find a value in a string and replace it with another value, the value may be (for example) £x on one line and £x.xx on the next, but will always be replaced by a value of £x.xx. Hope that makes sense? TIA.

Use this regex to match the prices: £[0-9]+\.?[0-9]{0,2}
You can perform the replace with this by using this function:
preg_replace("/£[0-9]+\.?[0-9]{0,2}/", " ** REPLACEMENT ** ", $input_lines);
The nice thing about preg_replace is that if you feed it a single line you'll get a single value back, but if you feed it an array of strings with the values you want changed it will return an array with those values replaces. Nice and tidy if you need to make a lot of replacements.

Related

Regex that finds JSON string inside another string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I would like to extract a JSON string within another string.
Currently I am getting the full string using file_get_contents and running the following pattern on the string: https://regex101.com/r/5jAucO/1
which pretty much gives multiple matches.
I would like to extract the JSON string that is saved in window._sharedData but haven't been able to achieve that. Does someone have any idea how I could do that?
Why not include _sharedData in the regex like?
_sharedData\s*=\s*\{.+\}
or with lookbehind:
(?<=_sharedData\s=)\s*\{.+\}
or take the json from a capturing group:
_sharedData\s*=\s*(\{.+\})
One concern with the lookbehind is if they add an additional whitespace character between _sharedData and = it won't match.
This only works well since there are no linebreaks in the JSON.

PHP Parse string to array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have this string
"{"(2,\"B - Tuna rungu\")","(8,\"C1 - Tuna grahita ringan\")"}"
and I wan't to convert this to PHP array, How do I do that in PHP? Is there any built in class that I can use for this?
the array of this string in PHP should be:
[[2,"B - Tuna rungu"], [8,"C1 - Tuna grahita ringan"]]
There is no class that you can use. You can try some simple string replaces but this is not really stable:
"{" -> [
"}" -> ]
etc.
But what is if you had "{" as proper value in a text field? The correct way is to create a parser/lexer for the syntax of the given string. But for this you need a complete defined syntax with all special cases.
Some information about this:
http://www.codediesel.com/php/building-a-simple-parser-and-lexer-in-php/

php remove empty single comment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have code like this
function get_id(){
return 1;
//
}
I want to remove all // inside the source code, but only those stand by it own, on a newline just like the example here. Nothing much nothing more. How can I do it?
Search using this regex:
^\s*\/\/\s*$
and replace by empty string.
If using PHP you can use \h (horizontal space) instead of \s:
$code = preg_replace('~^\h*//\h*$~m', '', $code);

PHP Type Casting a string to interger [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am parsing a file and pulling data out of it. One of the pieces of data is a decimal such as 1.9.
For some reason when I tried to save to my MySQL table it kept saving as 0. The field type is Decimal (6,2) in the table.
I attempted to manually cast it using various php functions such as...
settype(), (int), (float), (double) etc. Everytime I cast it, it would set it to zero or boolean. I used gettype to check it.
When parsing it I used trim() and str_replace() functions to remove spaces. What is odd is that I am using same method for pulling other values/decimals out of same file and php handled the casing itself.
Any ideas what could possible prevent me from casting this as a a number?
So this is what solved my problem for anyone who runs into this situation. Not sure it is the best way to handle it but it worked.
$dim = preg_replace("/[^0-9.]/", "", $dim)

Strip HTML tags AND double quotes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to clean my entries that look like
“If you think adventure is dangerous, try routine; it is lethal.”
<p>Life isn't about finding yourself it's about creating yourself. </p>
I need to remove HTML tags and double quotes and single quotes if they are in the beginning or the end of the string.
There was a function, but I don't remember what it was or something like a class...
You can remove everything with ^[“"'‘]|[”"'’]$|^<[^>]*?>|<[^>]*?>$
Here is a demo.

Categories