I want to take the values from a sql file with lots of inserts like this:
INSERT INTO "Messages" VALUES(7662,1,7333,'#foxy.giulia/$marcobonetti7;f966f2547124846f','marcobonetti7','marcobonetti7',NULL,X'6DF3228A75EEF69EB124C51BD0CA6EB7F3C35B33DC8062F479B60AC80442C21A','marcobonetti7',1386795144,61,NULL,0,NULL,NULL,NULL,NULL,'Domani durante il giorno sei a casa?',NULL,NULL,NULL,2,NULL,3,4,1,NULL,NULL,NULL,1198575,3063654961,309438692,NULL,NULL,NULL,NULL);
Complete SQL file is provided in this pastebin.
I have made this far preg_match_all('/insert into "([a-zA-Z]+)" values\((.*)[\);]{0}/isu'), but it doesn't take in account \r\n from Values(.*) and it doesn't detect it.
I need the values and the table name from a .sql file. The values can be only an entire string.
This regular expression will match the table name and the whole list of values as string:
preg_match_all('/insert into "([a-zA-Z]+)" values\((.*?)\);/isu', $string, $matches, PREG_SET_ORDER);
After that you could split the values with:
foreach ($matches as $key => $match) {
$matches[$key][2] = explode(',', $match[2]);
}
Related
How do I separate my array strings delimiter (|) using the implode function of PHP something like the below String
|Java||PHP||Bootstrap||HTML||CSS|
Actually, I am using a double delimiter to differentiate tags like SQL and MySQL because LIKE "%sql%" will return MySQL results as well. Should be LIKE "%|sql|%"
What I have tried:
$array_service_offer = array();
if (isset($_POST['service_offer'])) {
foreach ($_POST['service_offer'] as $selectedOption) {
array_push($array_service_offer, $selectedOption);
}
//$service_offer = implode(',',$array_service_offer);
$service_offer = '|' . implode('||', $array_service_offer) . '|';
} else {
$service_offer = "";
}
First of all, according to #Qirel comment, I would also recommend to use $array_service_offer[] = $selectedOption; instead of array_push($array_service_offer, $selectedOption);
now for separation, there are several solutions.
One solution is that:
1- to remove first and last | character (it is like trimming)
2- to explode the trimmed string using || delimiter
for that you may use the following code:
$service_offer_trimmed = preg_replace("~(^\|)|(\|$)~", "", $service_offer);
$service_offer_array = explode('||', $service_offer_trimmed);
The other solution is to use straight forward preg_replace function to separate the string. the command follows:
$service_offer_array = preg_split("~(^\|)|(\|\|)|(\|$)~", $service_offer, 0, PREG_SPLIT_NO_EMPTY);
And one more professional solution is that to store your data in database in JSON format rather than delimited code and then when you need to search in your database you may use MySql JSON_CONTAINS function rather than LIKE command.
I have not personally made a performance check on both two solutions but if it not a big database, then it is not a big concern as well.
Therefore, you initial code to get the data and store it into the database will be:
$array_service_offer = array();
if (isset($_POST['service_offer'])) {
foreach ($_POST['service_offer'] as $selectedOption) {
$array_service_offer[] = $selectedOption;
}
}
// $json_service_offer will be saved to the database
$json_service_offer = json_encode($array_service_offer);
the manual on how to use JSON_CONTAINS is in the following link:
12.17.3 Functions That Search JSON Values
A php script I have is extracting data from an XML file to a MySQL database.
It is set-up as followed:
$sql = "INSERT IGNORE INTO `tablename` (columnname) VALUES ('text text text text text text $variablename text text $variablename2 text text')
$variablename and $variablename2 are both variables being extracted from the XML file, and have varying lengths.
So, I know, if I was just dealing with PHP I could use strlen or a variation of (see How to Truncate a string in PHP to the word closest to a certain number of characters? but 'columnname' in my sql database is not a variable.
And 'columnname' is what is limited to the characters.
I set this in mySQL to VARCHAR MAX=106 -- and that works --
But when re-outputting this to my web host it takes the data from the SQL database which is stopped at the maxlength cut-off mid-word.
I want it so that if it does reach the maxlength, the last word is just removed.
Could this be done, perhaps when inputting into the SQL table?
Or even perhaps in the PHP file outputting back to my web host?, such as here:
$rs->data_seek(0);
while($res = $rs->fetch_assoc()) {
$a_topic = array(
"columnname" => $res["columnname"]
or maybe in the $params here?
$params = array(
'status' => $share['columnname'],
Big thanks!
function cut($string, $maxLength)
{
$revertString = strrev($string);
$spaceRevertedPos = strpos($revertString, ' ', strlen($string)-$maxLength);
$revertedCutString = substr($revertString, $spaceRevertedPos);
return strrev($revertedCutString);
}
DEMO
I am currently trying to filter through an Input string to find the single hashtags that a user wants to be displayed with his photo. However, I am currently getting inserts in my database that are not correct.
The best case scenario would be that every single hashtag is saved in a new database row with the photo id. However, I do not really know what to do to accomplish that.
$hashtag = new Hashtag;
$hashtag->photo_id = $photo->id;
$hashtag_string = Input::get('hashtags');
$hashtag_string = Str::contains($hashtag_string, '#')
$hashtag->hashtag = $hashtag_string;
$hashtag->save();
I found some functions in this cheat sheet (http://cheats.jesse-obrien.ca) but I do not get them to work properly.
Try this:
$str = $hashtag_string;
preg_match_all('/#(\w+)/', $str, $matches);
foreach ($matches[1] as $hashtag_name) {
$hashtag = Hashtag::firstOrCreate(array('hashtag' => $hashtag_name));
}
You could then, in this foreach loop, connect those hashtags to a post (or in your case a photo) or sth.
this is a code to catch all url in the POST and shorting them then insert each of them in row in mysql ..... but here it is inserting all the url in one row ??
so how can i make it catch the first url then insert it in database then go back for the second one and do the same..???
$urlinput=mysql_real_escape_string($_POST['url']);
$pattren="/(http:\/\/)[a-zA-Z0-9]*\.[a-z]*(.*)|(www)\.[a-zA-Z0-9]*\.[com]*(.*)/";
preg_match_all( $pattren, $urlinput, $matches );
foreach($matches[0] as $match) {
$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$sql = "insert into url values('$id','$match','$shorturl')";
mysql_query($sql,$con);
}
Here http://php.net/manual/en/function.preg-match-all.php you can read about the 4th parameter of preg_match_all. You can loop over the urls found. I changed the end of your regular expression, so it won't catch the whole line:
$urlinput=mysql_real_escape_string($_POST['url']);
$pattren="/(http:\/\/)[a-zA-Z0-9]*\.[a-z]*(.*)|(www)\.[a-zA-Z0-9]*\.[com]*([a-zA-Z0-9\.\-_\/\?=\:]*)/";
preg_match_all( $pattren, $urlinput, $matches, PREG_SET_ORDER );
foreach($matches as $match) {
$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$sql = "insert into url values('$id','" . mysql_real_escape_string($match[0]) . "','$shorturl')";
mysql_query($sql,$con);
}
Also be careful with SQL injection, and use mysql_real_escape_string when you use user data in your queries.
well, "(.*)" in your regex matches everything after it has found the beginning of a url. so if you're expecting multiple urls in one string, or coma separated or what not, they will all be bundled together.
You need to be able to split your input first, and then validate that each element is a valid url before you pick it up for insertion.
I am trying to create a php script that inputs the HTTP links pasted into a textarea into a separated row in a database. More exactly:
First page is where the textarea (name=textarea-linkfield) is, links are going to be pasted here
http://www.stackoverflow.com
http://www.yahoo.com
....
http://google.com
The links are being carried over into the php script with $linkfield=$_POST['textarea-linkfield']; and I would like the links to be inserted into the database, each http link per row. Database name: site, table name plow, columns: id, url, ...
L.E. I tried just as proof of concept:
$linkfield=$_POST['textarea-linkfield'];
$strip=explode("\n",$linkfield);
echo $strip[1];
but I get 500 INTERNAL SERVER ERROR
L.E.2
The answer:
// Split the string into pieces
$pieces = explode("\n", str_replace(array("\n", "\r\n"), "\n", trim($linkfield)));
// Build the top of the INSERT query
$sql = "INSERT INTO `plow`(`url`) VALUES\n";
// Build the rest of the ;INSERT query by re-assembling the
// pieces.
$sql .= "('";
$sql .= implode("'), ('", $pieces);
$sql .= "')";
mysql_query($sql) or die ('Error: ' . mysql_error());
mysql_close();
Thanks to all for their help.
Chris.
Depending on your os a newline can be "\n", "\r\n" or "\r".
Give this a shot:
$strip=explode("<br>", nl2br($linkfield));
or maybe safer:
$strip=explode("\n", str_replace(array("\n", "\r\n"), "\n", $linkfield));
use preg_match to find each URL and add it to the database.
Example 3 on this page should do the trick: http://php.net/manual/en/function.preg-match.php
this way you can enter URLs without having to use a new line.
if you're only using URLs, then you could also add a delimeter after each URL i.e. a comma (which isnt used in a URL) to explode them with using the explode() idea in the comment.
Here is your question, answered:
http://bytes.com/topic/php/answers/850719-extracting-individual-lines-multiline-text-box-processing