Hong
Hong

Reputation: 67

php get string in between character

I want to get thisisafirstname from the string below and put it in to another table. Here is how the strings are like in MySQL:

{\"label\":\"Name\",\"value\":\"thisisafirstname\",\"identifier\":\"field3\",\"type\":\"oneLineText\",\"page\":1,\"page_name\":\"Step 1\",\"width\":\"50%\"},

Here is what i did:

Function.php //works fine

function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

Main.php //Here is where I had problems

//Get Data
$sql = "SELECT content FROM wp_formcraft_3_submissions";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
//Output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $row1 = $row["content"];
//Get INFO in between
        require_once('function.php');
        $parsed = get_string_between($row1, '{\"label\":\"Name\",\"value\":\"', '\",\"identifier\":\"field3');
        echo $parsed;

Error Message:

Notice: Undefined variable: row2 in C:\Users\Admin\Desktop\connect\test.php on line 25

I think the problem are causing by the special characters. I tested out once with a simple email string and it works fine as I got gmail as result. Here is the code:

require_once('function.php');
$parsed = get_string_between($row1, "@", ".");
echo $parsed;

Upvotes: 0

Views: 67

Answers (4)

AbraCadaver
AbraCadaver

Reputation: 78994

It doesn't answer you parse error, but this is mostly JSON. This is the approach you should use, much easier:

$parsed = json_decode(trim(stripslashes($row1), ','), true);
echo $parsed['value'];

This would be easier if it was inserted into the DB properly:

  • The quotes are escaped so you need stripslashes()
  • There is a trailing comma so trim() maybe

Upvotes: 1

SpongePablo
SpongePablo

Reputation: 890

You are missing a semicolon:

Main.php

//Get Data
$sql = "SELECT content FROM wp_formcraft_3_submissions";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
//Output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $row1 = $row["content"];
//Get INFO in between
        require_once('function.php');
        $parsed = get_string_between($row1, '{\"label\":\"Name\",\"value\":\"', '\",\"identifier\":\"field3');
        echo $parsed;

Upvotes: 1

Scott
Scott

Reputation: 1922

You have a syntax error at line 25, you are missing a semi-colon. It should be:

$parsed = get_string_between($row1, '{\"label\":\"Name\",\"value\":\"', '\",\"identifier\":\"field3');

The interpreter expects the lexical element ; to proceed any assignments. The element it encounters without the semi-colon is your echo command, which throws the error that you are receiving.

Upvotes: 1

Jessica
Jessica

Reputation: 7005

Your best bet is to unserialize the data, then you can easily access the data you need. It looks like JSON but it's not a complete JSON string. Is there more to the data? If it's JSON, use json_decode to parse it and turn it into an array or object.

Upvotes: 0

Related Questions