Extracting strings based on a particular split delimiter in php - php

I have a string "test#html.com+test+7".I want to extract test#html.com, test and 7 into an array. How to achieve this is PHP? I have tried using preg_match but couldn't crack it.

print_r(explode("+","test#html.com+test+7"));
working code :)

Related

Splitting Text in PHP [duplicate]

This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
I am trying to make a card reader for cardcast. I get these cards in a string using file_get_contents and then need to extract the text for each card.
Here is an example piece of the string for one card, each one of these cards is separated by a comma and has the parentheses on either end.
{"id":"030b3406-55ae-4159-9129-04463c61973c","text":["Why does this not work? ",""],"created_at":"2017-06-04T15:13:42+00:00","nsfw":true}
I am trying to extract just the information inside the text tag, in this case ' Why does this not work? ","" ' (minus the single quotes and white space on both ends).
Could someone please help to extract this information using split or regex? I could do this slowly with split but I assume it is more efficient to use regex as there could be multi-hundred cards and this needs to be time efficient.
I looked into preg_match() but did not understand it well enough to get a functioning version.
Thanks
Looks like a JSON encoded text.
Simply decode the text using 'json_decode()'
JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
You can learn more about json here :
http://www.json.org/
https://www.w3schools.com/js/js_json_intro.asp
change json to php array with -> $array=json_decode($json,true)
get ur text attribute:
$array['text']

Splitting up a string with no common delimiter

I am trying to split up a string like this:
22/9 14 ALTERNATE (16) myXMG fe (2)infernoHitbox Arena
I want to get 22/9 14 then ALTERNATE then (16) etc. But also need to split (2) inferno and Hitbox Arena. Problem is theres no common delimter across the string to get all these out. Also, I will need to split other strings in a similar fashion. Same order of information, just different content..
I'm struggling for ideas. Any help would be appreciated!
This works only with a regular Expression. You could work with substr but that only works if the positions are the same.
So you should have a look at preg_match.
If all the data you got has the same Character indexes.
You should try this method
http://php.net/manual/en/function.substr.php
And also found one here with a similar question
separate string in two by given position

how to parse data already stored in database - PHP

I have a form users fill out that has one textarea. This gets stored in the database.
Now I want to sift through the data collected, get meaningful information out (like title) and store those in new fields in the database.
I know how to get the data out and store it in a variable. I'm not sure what to do next. I need to search the data for specific words like "title" and "instructions" and then focus in on everything after that word until a return was entered. Where should I start with this?
UPDATE
I really like the help so far, the expression match and preg_match_all / preg_match seem to get me started. One last question - I get how to find the start of the search, but how would I find the end? Usually the end will be denoted by a return (enter was pressed)
If there's any consistency to the text you can use a regular expression.
Otherwise you may want to update the form. Having to rely on consistent user input in a text field is iffy at best.
First of all, get data to a variable, then search in this variable to extract your data. The best way to do this is with Regular expression and with php functions preg_match and preg_match_all
EDIT:
For your second question you can search with regular expression to find \r and \n characters.
you can achieve this by using Explode() function.
Split the fields by using the separator. After converting the string to array, then you can parse the array again to get the Heading and value.
preg_match_all would be a good starting point.

Splitting a URL

I'm trying to split the following URL format: https://www.facebook.com/media/set/?set=a.10150495063500716.644503.10150093906460716&type=3
I'm trying to get the first number, between 'a.' and the next '.' I've been playing around with preg_match, but to no avail.. I'm not experienced at all with regex so perhaps I'm just using incorrect method or syntax. My attempts result in each character becoming an array key. If there's a simpler method than using regex then I'm all ears, was just pointed in this direction; all I'm needing is the number.
Any and All help is appreciated.
Without knowing the exact language you're using to solve this problem the following expression may do what you want:
"a[.](\d+)[.]"

PHP Regex Match all string in other file

I have created one regex that can extract all string from PHP files.
Example, I have "abc.php", I want to extract all string inside there (including tags " ' ).
I make my own regex but some of string didn't match or overmatch.
Note : My intention also same with post here -> PHP: Regex to match the following string samples
But agent-j answers inside that thread also didn't match some of string.
Basically, this is my regex
/[\"|\'][^.\/\"](.*?)[^,\\][\"|\']/
Here the problem in picture..
I also try use agent-j regex, but his regex has problem when matching string in multiple line.
His regex
(['"])((?:\\\1|(?!\1).)+)\1
Problem with this regex
The easiest way I have ever found to regex match any logic to an entire file has been to use
$something_better = explode(''',$something);
This way you get an array of data that is more easily evaluated. I use this concept every time I want to guarantee I can make the match exactly how I want every time.
What I would do here is to explode and extract the info between single quotes, matching what I wanted from them, and then implode on the single quote. Since you also want the double quotes, you can then explode and repeat the process for double quotes.
In my experience it is very hard to regex all your problems away in one simple statement. It's better to take it in smaller pieces if you can. There will be less room for error.
Look like anybody don't solve my problem.
I solved this problem myself with help of my friend.
So this is the regex that i was looking for.
/([\"\'])(?:(?=(\\\\?))\\2.)*?\\1/s

Categories