dtdvlazd56637 2017-06-07 21:05
浏览 18
已采纳

PHP设置id的值[复制]

This question already has an answer here:

I have some text containing variable data.

My age is: <span class="var" id="age">54</span> and my name is <span class="var" id="name">Matt</span>

If no url paramater is set then the page uses 54 which is correct, however if I set age in the url such as ?age=20 then I would like the age to be set to 20.

The text is from the database and is set to a variable called $content.

Not a duplicate. The text comes from the database and only needs to be replaced is a url param is set

</div>
  • 写回答

1条回答 默认 最新

  • dtb81443 2017-06-07 21:15
    关注

    Since you have a "condition" for the age to be displayed, therefore you will have to use PHP if else to check the date. Below is the code that you can use along with the explanation.

    Assuming that the below is the string what you get from your db field.

    $fetched_string = '<p> My Smith from <span class="var" id="town">Manchester, aged, <span >class="var" id="age">54</span>';
    

    Now you have to extract the age, which is always in between id="age"> and </span>

    The best way to do this is by creating a PHP function for this.

    function str_between($string, $searchStart, $searchEnd, $offset = 0) {
            $startPosition = strpos($string, $searchStart, $offset);
            if ($startPosition !== false) {
                $searchStartLength = strlen($searchStart);
                $endPosition = strpos($string, $searchEnd, $startPosition + 1);
                if ($endPosition !== false) {
                    return substr($string, $startPosition + $searchStartLength, $endPosition - $searchStartLength);
                }
                return substr($string, $startPosition + $searchStartLength);
            }
            return $string;
        }
    

    Now you simply have to pass the string values on the left (which will be the searchStart) and the right (searchEnd) of the fetched age. Below is the code to do this.

    $fetched_age = str_between($fetched_string, 'id="age">', '</span>');
    

    Based on this, I have revised the final code that you can use as is.

    //To get the data from a URL, $_GET method is used. First you will check if the "age" has been set in the URL or not. This is done by:
    
    function str_between($string, $searchStart, $searchEnd, $offset = 0) {
        $startPosition = strpos($string, $searchStart, $offset);
    
        if ($startPosition !== false) {
            $searchStartLength = strlen($searchStart);
            $endPosition = strpos($string, $searchEnd, $startPosition + 1);
    
            if ($endPosition !== false) {
                return substr($string, $startPosition + $searchStartLength, $endPosition - $searchStartLength);
            }
    
            return substr($string, $startPosition + $searchStartLength);
        }
    
        return $string;
    }
    
    //Fetched String for db
    $fetched_string = $row['full_string'];
    
    //This will give you the age
    $fetched_age = str_between($fetched_string, 'id="age">', '</span>');
    
    if (isset($_GET['age'])) {
        //If age is set in the URL, assign it to the PHP Variable
        $age_display = $_GET['age'];
    }
    else {
        //If age is not set, then assign the age fetched from the db.
        $age_display = $fetched_age;
    }
    

    Is this what you are looking for?

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题