skip to Main Content

table arts column title – example:

1122 A TITLE
025 - ANOTHER TITLE
1/7 - SOME TITLE
1 4 5 - SOME ANOTHER TITLE

I need to remove everything before the real title.

Characters for removing are – everything except letters (titles should start with a letter).

So I need something like:

update arts set title = replace(title, 'everything except letters on the beginning', '');

Using php or mysql inside phpmyadmin.

Any help?

3

Answers


  1. Use regex to match anything up to first letter, then capture the rest.

    $arr = ["1122 A TITLE",
    "025 - ANOTHER TITLE",
    "1/7 - SOME TITLE",
    "1 4 5 - SOME ANOTHER TITLE"];
    
    foreach($arr as $val){
        preg_match("/.*?([a-zA-Z].*)/", $val, $m);
        echo $m[1] . "n";
    }
    

    Output:

    A TITLE
    ANOTHER TITLE
    SOME TITLE
    SOME ANOTHER TITLE
    

    https://3v4l.org/4sZ9I

    Login or Signup to reply.
  2. You can filter it out with a regular expression:

    $matches = []
    if (preg_match("/^([0-9-_/ ]{0,})(.{0,})/", $string, $matches)) {
        $filtered = $matches[2];
    }
    
    Login or Signup to reply.
  3. I think it is better to do all these operations at PHP side, because mysql does not support this, If you really want to do this at MySQL side then you need to write your own custom function,

    Try with PHP code as follow.

    $string = '1 4 5 - SOME ANOTHER TITLE';
    $pattern = '/^[^a-zA-Z]{1,}/';
    $replacement = '';
    $title = preg_replace($pattern, $replacement, $string);
    
    $qry = 'update arts set title = "'.$title.'"';
    
    echo $qry;
    

    Above code will generate output as follow

    update arts set title = “SOME ANOTHER TITLE”

    Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search