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.
Related
I have an array element that contains a subpage's content:
$page['content']
I want to do some MySQL query, if the variable's content contains this pattern: {gallery:somerandomIDnumber}
The problem is, I don't want to lose the other stuff, and it's also important that the query should run where the pattern belongs.
For example, this is the $page['content'] content:
<h1>Title of the page</h1>
{gallery:10}
<p>Other informations...</p>
I tried this with preg_match function, but unfortunately I can't figure it out, how I can save the other content around my {gallery:10} pattern.
// Gallery include by ID
preg_match('~\{gallery\:[0-9]{1,5}\}~', $page['content'], $matches);
foreach($matches[0] as $value) {
$int = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
$query = 'SELECT * ';
$query .= 'FROM gallery_images ';
$query .= 'WHERE gid = '.$int;
$gallery = ms_query($query); //ms_query is a function I wrote myself. Unlike mysqli_query function this function doesn't require the connection parameter every single time I call it, only the query itself
while($gimage = mysqli_fetch_assoc($gallery)) {
echo '<img src="admin/uploaded/'.$gimage['imagepath'].'" width ="100">';
}
}
Summarazing, in this situation, I want to echo
the title of the page
some imagepath from my database
other informations
Thanks in return for Your help!
preg match will get you something in $matches only when u put it in ().
e.g. in your {gallery:10}
you will get the 10 if you make your preg like this;
preg_match('~{gallery:([0-9]{1,5})}~', $page['content'], $matches);
also echo your sql query and paste result
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]);
}
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.
I am trying to create file links based a variable which has a "prefix" and an extension at the end.
Here's what I have:
$url = "http://www.example.com/mods/" . ereg("^[A-Za-z_\-]+$", $title) . ".php";
Example output of what I wish to have outputted (assuming $title = testing;):
http://www.example.com/mods/testing.php
What it currently outputs:
http://www.example.com/mods/.php
Thanks in advance!
Perhaps this is what you need:
$title = "testing";
if(preg_match("/^[A-Za-z_\-]+$/", $title, $match)){
$url = "http://www.example.com/mods/".$match[0].".php";
}
else{
// Think of something to do here...
}
Now $url is http://www.example.com/mods/testing.php.
Do you want to keep letters and remove all other chars in the URL?
In this case the following should work:
$title = ...
$fixedtitle=preg_replace("/[^A-Za-z_-]/", "", $title);
$url = "http://www.example.com/mods/".$fixedtitle.".php";
the inverted character class will remove everything you do not want.
OK first it's important for you to realize that ereg() is deprecated and will eventually not be available as a command for php, so to prevent an error down the road you should use preg_match instead.
Secondly, both ereg() and preg_match output the status of the match, not the match itself. So
ereg("^[A-Za-z_\-]+$", $title)
will output an integer equal to the length of the string in $title, 0 if there's no match and 1 if there's a match but you didn't pass it another variable to store the matches in.
I'm not sure why it's displaying
http://www.example.com/mods/.php
It should actually be outputting
http://www.example.com/mods/1.php
if everything was working correctly. So there is something going on there, and it's definitely not doing what you want it to. You need to pass another variable to the function that will store all the matches found. If the match is successful (which you can check using the return value of the function) then that variable will be an array of all matches.
Note that with preg_match by default only the first match will be returned. but it will still generate an array (which can be used to get isolated portions of the match) whereas preg_match_all will match multiple things.
See http://www.php.net/manual/en/function.preg-match.php for more details.
Your regex looks more or less correct
So the proper code should look something like:
$title = 'testing'; //making sure that $title is what we think it is
if (preg_match('/^[A-Za-z_\-]+$/',$title,$matches)) {
$url = "http://www.example.com/mods/" . $matches[0] . ".php";
} else {
//match failed, put error code in here
}
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