PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How to remove a characters in string php

 November 20, 2022     php, preg-replace, replace, string     No comments   

Issue

I am stuck with this. I tried json_decode but it gives me an error.

How can I remove the "[]" and the ' " " ' characters of my string?

I have a string with this values ["Red Cat","Black Cat","Red Shoes"] i want to remove and make my string like these: Red Cat, Black Cat, Red Shoes

Here i tried to make like this:

$data = json_decode($display_data);

and also i tried something that I've searched: $data = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', ' ', $display_data);


Solution

From the comments: 'use json_decode'. Example:

$display_data = '["Red Cat","Black Cat","Red Shoes"]';
$data = json_decode($display_data); // Here we have array of strings
$result = implode(', ', $data); // Convert array back to one string

echo $result; // here we have "Red Cat, Black Cat, Red Shoes"


Answered By - Anton
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing