如何使用php过滤字符串?

时间:2017-03-15 22:23:08

标签: php regex

我有一个插件从我的数据库导出一堆字符串。返回的数据采用如下格式

a:3:{i:0;s:8:"Skeletal";i:1;s:6:"Cardio";i:2;s:8:"Muscular";}

a:3:{i:0;s:14:"Access to care";i:1;s:15:"Confidentiality";i:2;s:20:"Consent to treatment";}

我可以应用php函数来过滤数据,如何使用函数让它像这样返回。

Skeletal, Cardio, Muscular

Access to care, Confidentiality, Consent to treatment

3 个答案:

答案 0 :(得分:3)

这些字符串是序列化变量。特别是数组。使用join()获取数组,然后$unserialized_array = unserialize($string); $comma_separated = join(', ', $unserialized_array); echo $comma_separated; 以逗号分隔您想要的方式。

Traceback (most recent call last):
  File "C:\Python35\lib\tkinter\__init__.py", line 1559, in __call__
    return self.func(*args)
TypeError: UpdateSheet() missing 1 required positional argument: 'value'


from tkinter import *
from tkinter import ttk
import openpyxl


class Application(Frame):


    """GUI app to automate CR creation"""
    def __init__(self, master):
        """Initialize the Frame"""
        ttk.Frame.__init__(self, master)
        self.grid()
        self.create_widgets()




    def create_widgets(self):
        #create instruction label and Entry widget
        eng_font=('Lucida Grande', 11)
        #CR Number Entry Box
        self.cr_label = Label(self, text="CR Number: ", font=eng_font)
        self.cr_label.grid(row=0, sticky = E)
        self.cr_ent = Entry(self)
        self.cr_ent.grid(row=0, column=1, sticky=E)


        #create Submit button
        self.submit_bttn = Button(self, text = "Submit", command = self.UpdateSheet).grid(row=10, column=1, sticky=W)

        #Engineer Selection
        self.eng_name = Label(self, text="Select Engineer: ", font=eng_font).grid(row=3, sticky=E)
        variable=StringVar(self)
        variable.set("")
        eng_lst=OptionMenu(self, variable, 'Johnson, Bob', 'Doe, John', 'Doe, Jane')
        eng_lst.grid(row=3, column=1, sticky=W)

        #CR Start Date & Time
        self.date_lbl=Label(self, text="CR Start Date & Time", font=eng_font).grid(row=5,sticky=E)
        self.date_ent=Entry(self).grid(row=5, column=1, sticky=E)
        self.duration_lbl=Label(self, text="Change Duration", font=eng_font).grid(row=6, sticky=E)
        self.duration_ent = Entry(self).grid(row=6, column=1, sticky=E)

        #CR Type Selection
        self.cr_type_lbl = Label(self, text = 'CR Type', font = eng_font).grid(row=8, sticky=E)
        variableType=StringVar(self)
        variableType.set("")
        cr_type=OptionMenu(self, variableType, 'Gateway Upgrade', 'NIC Replacement', 'Interface Errors', 'Decomm', 'Failover')
        cr_type.grid(row=8, column=1, sticky=W)

    def UpdateSheet(self, value):
            wb=openpyxl.load_workbook('GatewayUpgrade2.xlsx')
            wb.get_sheet_by_name('Contacts')
            eng_name = value
            wb['A3'] = eng_name








#main
root=Tk()
root.title("CR Creation Tool")
root.geometry("400x250")

app = Application(root)

root.mainloop()

编辑:我认为这是最简单的解决方案,但是下面的黑曜石时代的答案提供了一个正则表达式,也可以做你想要的(你确实要求正则表达式);

答案 1 :(得分:2)

这些是序列化数组,您需要unserialize them

答案 2 :(得分:0)

您可以使用非常简单preg_match_all()的{​​{1}}:

来完成此操作
/".*?"/

现在<?php $string = 'a:3:{i:0;s:8:"Skeletal";i:1;s:6:"Cardio";i:2;s:8:"Muscular";}'; preg_match_all('/".*?"/', $string, $matches); print_r($matches); 包含一个数组,您只需访问以下索引:

$matches

我已经创建了这个here的工作样本。

希望这有帮助! :)