By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,170 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

How to get just the string in mysql ?

P: 10
hello all , i need help at this:
[php] <?
//connection stuff
$conexao = mysql_connect("localhost", "root", "12345")
or die ("error to db.");
$db = mysql_select_db("mal")
or die ("error selecting db.");

// some regex
$subject = $_POST['text'] ;
$pattern = '/def/';
preg_match($pattern, $subject, $matches);
$sql = "INSERT INTO tab1 (nome )
VALUES ('$matches') " ;

$sql = mysql_query($sql)
or die ("there was a mistake");
?>

<form action="mal.php" method="post">
<label for="texto">Texto: </label>
<textarea name="texto" id="text" rows="10" cols="30" />
</textarea><br />

<input type="submit" value="insert">

</form>
[/php]

Ok, lets imagine i put in the text box something like : abcdefg
and i just want : def to be returned to mysql
i dont know why but the only thing i get returned is : array lol
can anyone help?
Jul 31 '07 #1
Share this Question
Share on Google+
7 Replies


code green
Expert 100+
P: 1,726
Matches returns an array. You need to extract what you need from the array.

Expand|Select|Wrap|Line Numbers
  1. int preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )
Searches subject for a match to the regular expression given in pattern.
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
If you are comparing strings without special characters the string functions are quicker.
[PHP]strstr(), strpos()[/PHP]
Jul 31 '07 #2

P: 10
Sorry , im a newbye here..and i didnt understand what to do... :(
shall i do this?
...
preg_match($pattern, $subject, $matches[1]);
$sql = "INSERT INTO tab1 (nome )
VALUES ('$matches') " ;

...

can someone plese give me a concrete "how-to" put this code running?
Jul 31 '07 #3

code green
Expert 100+
P: 1,726
No that is wrong. $match returns an array.
ie
[PHP]
$subject = $_POST['text'] ;
$pattern = '/def/';
preg_match($pattern, $subject, $matches);
$text = $match[0];
$sql = "INSERT INTO tab1 (nome )
VALUES ('$text') " ;[/PHP]
But this makes no sense.
Why are you returning a string that you already know the value of?
Why not just send $pattern to the DB?
Jul 31 '07 #4

P: 10
this was just 1 example , i just want to understand how to input that string :)
the real code will be something like this :
[php]
<?
$conexao = mysql_connect("localhost", "root", "12345")
or die ("Erro na conex„o ao banco de dados.");
$db = mysql_select_db("mal")
or die ("Erro ao selecionar a base de dados.");


if( preg_match ('/(.+) \(ok\)\n/' , $_POST['texto'], $matches ) )
{
$sql = "INSERT INTO tabela1 (nome )
VALUES ($matches) " ;

if (!$result = @mysql_query($sql))
{
exit(mysql_error());
}
}

?>



<form action="mal.php" method="post">
<label for="texto">Texto: </label>
<textarea name="texto" id="texto" rows="10" cols="30" />
</textarea><br />

<input type="submit" value="inserir">

</form>
[/php]
and the things that users will input in the text box its something like : Jonh (ok) , greg (ok).
Jul 31 '07 #5

P: 10
and btw, i tested your ideia, but still, just get the array word in mysql
Jul 31 '07 #6

kovik
Expert 100+
P: 1,044
and btw, i tested your ideia, but still, just get the array word in mysql
Then do something about it. There are dozens are array functions, and you could turn an array into a string using a combination of any of them. A simple approach is the implode().
Aug 1 '07 #7

code green
Expert 100+
P: 1,726
and btw, i tested your ideia, but still, just get the array word in mysql
You obviously did not test my idea, you copied my text and hoped it would work. If you look again you can see I made a mistake.
this was just 1 example
Yes I knew that.
But I really think you are getting bogged down with preg_match when the string functions would be better.
Regular expressions are only necessary when dealing with HTML, XML for example..
If I am right, you want to extract the text occuring before the word 'ok'.
You can use a combination of strpos and substr for this
[PHP]$matches = substr($_POST['texto'],strpos($_POST['texto'],'ok' ))[/PHP]This may require some manipulation.
You may need to add the length of the string 'ok' to the value returned by strpos.
Aug 1 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.