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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置