How to get just the string in mysql ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blitzztriger
    New Member
    • Jul 2007
    • 10

    How to get just the string in mysql ?

    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($pat tern, $subject, $matches);
    $sql = "INSERT INTO tab1 (nome )
    VALUES ('$matches') " ;

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

    <form action="mal.php " method="post">
    <label for="texto">Tex to: </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?
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Matches returns an array. You need to extract what you need from the array.

    Code:
    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]

    Comment

    • blitzztriger
      New Member
      • Jul 2007
      • 10

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

      ...

      can someone plese give me a concrete "how-to" put this code running?

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        No that is wrong. $match returns an array.
        ie
        [PHP]
        $subject = $_POST['text'] ;
        $pattern = '/def/';
        preg_match($pat tern, $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?

        Comment

        • blitzztriger
          New Member
          • Jul 2007
          • 10

          #5
          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($s ql))
          {
          exit(mysql_erro r());
          }
          }

          ?>



          <form action="mal.php " method="post">
          <label for="texto">Tex to: </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).

          Comment

          • blitzztriger
            New Member
            • Jul 2007
            • 10

            #6
            and btw, i tested your ideia, but still, just get the array word in mysql

            Comment

            • kovik
              Recognized Expert Top Contributor
              • Jun 2007
              • 1044

              #7
              Originally posted by blitzztriger
              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().

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                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.

                Comment

                Working...