正则表达式...从字符串中提取某些单词

时间:2016-02-29 14:51:17

标签: php regex

我有以下字符串,我想从中提取位置。

 {"image_intro":"images\/slider\/lazic.jpg","float_intro":"","image_intro_alt":"Tuttlingen","image_intro_caption":"","image_fulltext":"images\/slider\/lazic.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}

所以我需要 Tuttlingen 这个词......仅此而已。

有人可以告诉我 PHP 的正确正则表达式吗?

2 个答案:

答案 0 :(得分:5)

这是一个格式很好的JSON字符串。使用json_decode()会更容易。

$string = '{"image_intro":"images\/slider\/lazic.jpg","float_intro":"","image_intro_alt":"Tuttlingen","image_intro_caption":"","image_fulltext":"images\/slider\/lazic.jpg","float_fulltext":"","image_fulltext_alt":"","image_fulltext_caption":""}';
$json = json_decode($string, true);
echo $json['image_intro_alt'];

答案 1 :(得分:1)

#include <iostream>
#include <memory>
#include <string>

using namespace std;

class Popup
{
public:
    Popup(int value, string str){
        this->v = value;
        this->str = str;
    }
    virtual void print() = 0;
    int v;
    string str;
};
typedef shared_ptr<Popup> PopupPtr;

class PopupA : public Popup
{
public:
    PopupA(int v, string str) : Popup(v, str) { }
    virtual void print() {
        cout << "PopupA" << endl;
    }
};
typedef shared_ptr<PopupA> PopupAPtr;

class PopupB : public Popup
{
public:
    PopupB(int v, string str) : Popup(v, str) { }
    virtual void print() {
        cout << "PopupB" << endl;
    }
};
typedef shared_ptr<PopupB> PopupBPtr;


class Builder
{
public:
    PopupPtr popupPtr;
    Builder() { };
    shared_ptr<Builder> init(int value, string str) {
        shared_ptr<Builder> builder;

        switch (value)
        {
        case 1:
            popupPtr = PopupAPtr(new PopupA(value, str));
            break;
        case 2:
            popupPtr = PopupBPtr(new PopupB(value, str));
            break;
        default:
            cout << "default error" << endl;
            break;
        }

        if (popupPtr) {
            builder = shared_ptr<Builder>(this);
        } 
        else {
            cout << "popup is null" << endl;
        }

        if (!builder) {
            cout << "builder is null" << endl;
        }
        return builder;
    }

    PopupPtr build()
    {
        if (!popupPtr) {
            cout << "popup is null" << endl;
        }
        return PopupPtr(popupPtr);
    }

};
typedef shared_ptr<Builder> BuilderPtr;

int main()
{
    BuilderPtr builderPtr = BuilderPtr(new Builder());

    PopupPtr popupPtr1 = builderPtr->init(1, "111111111111")->build();
    popupPtr1->print();

    PopupPtr popupPtr2 = builderPtr->init(2, "222222222222")->build();
    popupPtr2->print();
    return 0;
}

更新:根据建议使匹配延迟