SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    string replace how to

    I have some values stored in a database, sometimes it is only one sometimes several separated by a mysql linebrake, like

    instance where I only have a value

    name=value

    instance where I have more than 1 value

    name1=valuex
    name2=valuey
    etc

    I am trying to parse this, in the first case I am able to achieve it like this

    (bellow the value inside the mysql db)

    Style=somestyle

    $style_name = str_replace("Style=", "", $params_to_string);

    in the case above I am getting
    $style_name = somestyle;

    then I can use the string somestyle and do what I need to, but now I got to the place where there are more of this values in the db all have a different name and are separated by a linebrake, can someone give a sample on how to parse the one bellow?

    style=somestyle
    language=somelanguage

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Would you be better off cleaning up the data in the database?

    mysql REPLACE function.

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    What is the result you need from the parsing?

  4. #4
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Would you be better off cleaning up the data in the database?

    mysql REPLACE function.
    this is not an option for me, I am making a joomla template and joomla stores the parameters that way so I canīt just randomly modify the database, that will probably mess up the site, thank you anyway

  5. #5
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What is the result you need from the parsing?
    I only need the part after the =, so in the sample style=style1 I would need the string style1 in the sample language=en I would need the string en, the above example is stored in mysql like this

    Code MySQL:
    style=style1
    language=en

    It looks just like that, well the style and language can be anything, I am making those names so they can be anything I want, also the part after the = sign, but they are stored in that exact same way, so if I wanted to have a variation of styles like style1, style2 etc then the style field can be either style=style1 or style=style2 and I need that part after the = sign so I can use it in the code, I am able to achieve it when there is only 1 parameter, but if there is more than 1 I have no idea how to do it.

  6. #6
    SitePoint Member
    Join Date
    May 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there is more than 1 you have no idea how to do it, but why? is this too difficult?

  7. #7
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hrimsolutions View Post
    If there is more than 1 you have no idea how to do it, but why? is this too difficult?
    For me yes, I am learning and have not been able to figure how to do it, that is why I asked

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    PHP Code:
    $values explode("\n"$params_to_string);
    foreach (
    $values as $key => $value) {
      list(
    $paramname$paramvalue) = explode('='$value);
      echo 
    $paramname ' = ' $paramvalue;


  9. #9
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    PHP Code:
    $values explode("\n"$params_to_string);
    foreach (
    $values as $key => $value) {
      list(
    $paramname$paramvalue) = explode('='$value);
      echo 
    $paramname ' = ' $paramvalue;

    Thank you I will try that!

  10. #10
    SitePoint Addict tlacaelelrl's Avatar
    Join Date
    Apr 2011
    Location
    Mexico city, Mexico
    Posts
    353
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    PHP Code:
    $values explode("\n"$params_to_string);
    foreach (
    $values as $key => $value) {
      list(
    $paramname$paramvalue) = explode('='$value);
      echo 
    $paramname ' = ' $paramvalue;

    That worked thanks however I will not use it, as I was trying to figure it out, I found a document explaining how this can be done in joomla which is where I am trying to use it and it has a way of getting this easy, here is what I found in case anyone comes looking for the same

    $params->get('param_name');


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •