doushenmao9036 2016-04-06 07:38
浏览 176
已采纳

我可以从字符串中删除随机数字和字母。 但只保留重要的

Okey so i know that i can remove letters and numbers from a string by using preg_replace. but here is what im trying to do.

Im using a wordpress plugin called Engage forms, that stores submitted data in a mySQL database.

The tricky part is that the data that's being submitted is not stored cleanly for some reason. (reason being that the devs that made the plugin with some purpurs for this and i cant figuer out how they print out without the unknown numbers and letters.)

Here is what i mean:
I submit the data

  • Firstname: Peter
  • Lastname: Stormare
  • Email: storm@ren.com
  • Phone: 0736997385

Here is how the data inside the mySQL database looks and how its being printed out on the webpage:

Very messy

a:4:{s:18:"firstname25601459863853";s:5:"Peter";s:23:"lastnamn77151459863853";s:8:"Stormare";s:19:"email58511459863853";s:13:"Storm@ren.com";s:21:"phone22101459863853";s:10:"0736997385";}


My attempt at cleaning up the data

So ive tried using my little knowledge and take away as much as i can with:

$patterns = array();
        $patterns[0] = '/[{}]/';
        $patterns[1] = '/[a-x]:[0-9]/';
        $patterns[2] = '/[0-9]:/';
        $patterns[3] = '/[a-x]:/';
        $patterns[4] = '/[":"]/';
        $patterns[5] = '/;/';
        $patterns[6] = '/namn[1]/'; 
        $replacements = array();
        $replacements[2] = '';
        $replacements[1] = '';
        $replacements[0] = '';
        $replacements[3] = '';
        $replacements[4] = ' ';
        $replacements[10] = ''

            echo preg_replace($patterns, $replacements, $leadslist);

Which should output this now:

firstname25601459863853 Peter lastname77151459863853 Stormare email58511459863853 Storm@ren.com phone22101459863853 0736997385


The Big Question

Is there anyway i can now target the random numbers that follow firstname and the rest, without deleting the phone numbers? (the example phone number is 0736997385.


Possible solution

The best solution atm was submitted by user: Pei-turn.

He showed me how to remove any number bigger then 10 digits. Using this pattern in Preg_replace()

$patterns[3] = '/[0-9]{0,9}[0-9]{11,}/';

This works very well. Only thing im worried about is if some data might get removed due to weird future form submissions.

  • 写回答

1条回答 默认 最新

  • doutan1970 2016-04-06 07:49
    关注

    A phone number has 10 digits, try to remove all numbersequences shorter than 10 and longer then 10

    [0-9]{0,9} [0-9]{11,}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错