沃梦达 / 编程问答 / php问题 / 正文

将PREG_REPLACE更改为PREP_REPLACE_CALLBACK

Changing preg_replace to preg_replace_callback(将PREG_REPLACE更改为PREP_REPLACE_CALLBACK)

本文介绍了将PREG_REPLACE更改为PREP_REPLACE_CALLBACK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为这个案例寻找了其他结果,但我自己无法修复它。如果有人能帮助我将此preg_place替换为preg_place_allback

,我将不胜感激。
preg_replace("/[-_]([a-z])/e", "ucfirst('\1')", ucwords($verb));

<?php

/** @see Zang_Exception **/
require_once 'Exception.php';

/** @see Zang_Schemas **/
require_once 'Schemas.php';

/**
 * 
 * A Zang InboundXML wrapper.
 * 
 * Please consult the online documentation for more details.
 * Online documentation can be found at: http://www.zang.io/docs/api/inboundxml/
 * 
 * --------------------------------------------------------------------------------
 * 
 * @category  Zang Wrapper
 * @package   Zang
 * @author    Nevio Vesic <nevio@zang.io>
 * @license   http://creativecommons.org/licenses/MIT/ MIT
 * @copyright (2012) Zang, Inc. <support@zang.io>
 */

class Zang_InboundXML
{

    /**
     * InboundXML simple xml element container
     * 
     * @var null|SimpleXmlElement
     */
    protected $element;

    /**
     * Current child pointer. Used for nesting validations
     * 
     * @var string|null
     */
    protected $_currentChild = null;

    /**
     * Constructs a InboundXML response.
     *
     * @param SimpleXmlElement|array $arg:
     *   - the element to wrap
     *   - attributes to add to the element
     *   - if null, initialize an empty element named 'Response'
     */
    public function __construct($arg = null) {
        switch (true) {
            case $arg instanceof SimpleXmlElement:
                $this->element = $arg;
                $this->_currentChild = strtolower($arg->getName());
                break;
            case $arg === null:
                $this->element = new SimpleXmlElement('<Response/>');
                $this->_currentChild = 'response';
                break;
            case is_array($arg):
                $this->element = new SimpleXmlElement('<Response/>');
                $this->_currentChild = 'response';
                foreach ($arg as $name => $value) {
                    $this->_validateAttribute($name, 'response');
                    $this->element->addAttribute($name, $value);
                }
                break;
            default: throw new Zang_Exception('InboundXML Invalid construction argument');
        }
    }


    /**
     * Converts method calls into InboundXML verbs.
     *
     * @return SimpleXmlElement A SimpleXmlElement
     */
    public function __call($verb, array $args) {

        /** convert verbs input like-this-one to LikeThisOne **/
        $verb = preg_replace("/[-_]([a-z])/e", "ucfirst('\1')", ucwords($verb));

        /** Let's first go check if the verb exists **/
        $this->_validateVerb(ucfirst($verb));

        /** Let's go validate nesting **/
        $this->_validateNesting(ucfirst($verb));

        list($noun, $attrs) = $args + array('', array());

        if (is_array($noun)) list($attrs, $noun) = array($noun, '');

        $child = empty($noun)
            ? $this->element->addChild(ucfirst($verb))
            : $this->element->addChild(ucfirst($verb), $noun);

        foreach ($attrs as $name => $value) {
            /** Validation of verb attributes **/
            $this->_validateAttribute($name, $verb);
            $child->addAttribute($name, $value);
        }
        return new self($child);

    }


    /**
     * Returns the object as XML.
     *
     * @return string The response as an XML string
     */
    public function __toString() {
        $xml = $this->element->asXml();
        return str_replace(
            '<?xml version="1.0" ?>', 
            '<?xml version="1.0" encoding="UTF-8" ?>', 
            $xml
        );
    }


    /**
     * Validate existance of the verb. Return true if exists, throw exception
     * if fails.
     * 
     * @param  string $verb
     * @throws Zang_Exception 
     * @return bool
     */
    private function _validateVerb($verb) {
        $schemas = Zang_Schemas::getInstance();

        if(!$schemas->isVerb(ucfirst($verb))) {
            $available_verbs = implode(', ', $schemas->getAvailableVerbs());
            throw new Zang_Exception(
                "Verb '{$verb}' is not a valid InboundXML verb. Available verbs are: '{$available_verbs}'"
            );
        }

        return true;
    }


    /**
     * Validate if previous child allows this verb to be its child.
     * 
     * @param  string  $verb
     * @return boolean
     * @throws Zang_Exception 
     */
    private function _validateNesting($verb) {
        $schemas = Zang_Schemas::getInstance();

        if(!$schemas->isNestingAllowed(ucfirst($this->_currentChild), ucfirst($verb))) {
            $nestable_verbs = implode(', ', $schemas->getNestableByVerbs(ucfirst($this->_currentChild)));
            $current_verb   = ucfirst($this->_currentChild);
            $next_verb      = ucfirst($verb);
            throw new Zang_Exception(
                "InboundXML element '{$current_verb}' does not support '{$next_verb}' element. The following elements are supported: '{$nestable_verbs}'."
            );
        }

        return true;
    }


    /**
     * Validate if attribute of verb exists. If not, throw exception, otherwise, return true.
     * 
     * @param  string $attr
     * @param  string $verb
     * @return boolean
     * @throws Zang_Exception 
     */
    private function _validateAttribute($attr, $verb) {
        $schemas = Zang_Schemas::getInstance();

        if(!$schemas->isValidAttribute($attr, ucfirst($verb))) {
            $verb_attribuges = implode(', ', $schemas->getAvailableAttributes(ucfirst($verb)));
            throw new Zang_Exception(
                "Attribute '{$attr}' does not exist for verb '{$verb}'. Available attributes are: '{$verb_attribuges}'"
            );
        }
        return true;
    }

}

推荐答案

不太清楚为什么要使用其他函数。它可能是这是因为您正在升级php,并且不再支持e修饰符。并且您发布的示例代码建议您正在尝试将连接的表达式转换为驼峰大小写...

看看这个简单的演示:

<?php
$verb = 'this is a simple-test_sentence to convert.';
echo preg_replace_callback(
    "/[-_]([a-z]+)/",
    function($string) { return ucfirst($string[1]); },
    ucwords($verb)
);

输出显然是:

这是要转换的SimpleTestSentence。

这篇关于将PREG_REPLACE更改为PREP_REPLACE_CALLBACK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将PREG_REPLACE更改为PREP_REPLACE_CALLBACK

基础教程推荐