0

I am currently getting the URL from an uploaded file on wordpress contactform7, for some reason the URL is being shown like this

["https:\/\/MYDOMAIN.com\/wp-content\/uploads\/wpcf7_drag-n-drop_uploads\/useruploads\/Jordan-2.pdf"]

how can i convert this to look like a proper domain without all of the extra slashes brackets and speechmarks?

Thanks,

Tried string replace, but got confused. new to php

  • 2
    That is JSON, use `json_decode`. e.g. `echo json_decode('["https:\/\/MYDOMAIN.com\/wp-content\/uploads\/wpcf7_drag-n-drop_uploads\/useruploads\/Jordan-2.pdf"]', TRUE)[0];` – user3783243 Jun 22 '23 at 23:47
  • Using str_replace() to remove those extra slashes and brackets is a simple and effective solution. JSON decoding functions are typically used when dealing with valid JSON data structures, where decoding is required to access the actual values. In this case, the provided string doesn't require JSON decoding, as it is not a JSON-encoded value. By using str_replace(), you can directly address the issue at hand without the need for additional processing or unnecessary complexity. – ELMEHDI ACHAHED Jun 23 '23 at 00:01
  • It's JSON, as stated above. Demo: https://3v4l.org/WS1MC – ADyson Jun 23 '23 at 00:22
  • 2
    This is a duplicate of [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – ADyson Jun 23 '23 at 00:47
  • Hi, it seems it's not JSON. Was getting an error and did var_dump and it gave me this error : {code: 'invalid_json', message: 'The response is not a valid JSON response.'} – Jamie Warren Jun 27 '23 at 20:53

1 Answers1

-1

Hi i hope you are doing well , as i can see you are trying to remove the extra backslashes from your domain you can use two methods pick whatever you want .

  1. Method 1: Using stripslashes that will directly remove the extra backslashes, $url = stripslashes('https:\/\/MYDOMAIN.com\/wp-content\/uploads\/wpcf7_drag-n-drop_uploads\/useruploads\/Jordan-2.pdf');
  2. Method 2: You said that you tried string replace and find it hard i will explain how string replace work. the str_replace function is used to replace specific parts of a string with new values.
    as you can see it takes 3 parameters str_replace('search', 'replace', $originalString); It scans the original string and replaces all occurrences of the search string with the corresponding replacement string. The modified string is then returned as the output.

this is the code you are gonna use :

$url = '["https:\/\/MYDOMAIN.com\/wp-content\/uploads\/wpcf7_drag-n-drop_uploads\/useruploads\/Jordan-2.pdf"]';
$url = str_replace(['"', '\\'], '', $url);
echo $url;

What I recommend is to use method 1 because it is easier to handle and to use.
I explained method 2 (str_replace) to you because I noticed that you had some difficulty with it.

I hope you find this information useful, even though my limited experience.
have a good day :)

  • 2
    No, please don't reinvent the wheel. This is JSON encoded, it should be decoded using relevant function. – user3783243 Jun 22 '23 at 23:47
  • ["https:\/\/MYDOMAIN.com\/wp-content\/uploads\/wpcf7_drag-n-drop_uploads\/useruploads\/Jordan-2.pdf"] is not a JSON-encoded value that requires decoding. It is a string representation that includes unnecessary escape characters and brackets. – ELMEHDI ACHAHED Jun 22 '23 at 23:59
  • 2
    @ELMEHDIACHAHED it's a json array with one element, which is a string. If you don't believe us, here's a demo: https://3v4l.org/WS1MC . – ADyson Jun 23 '23 at 00:20
  • Neither of these methods you've suggested will remove the brackets or quote marks: https://3v4l.org/AJSSB, https://3v4l.org/nZXtr . Did you actually test them before answering? – ADyson Jun 23 '23 at 00:26
  • @ADyson Hi i hope you are having a good day , yeah i have test them and they are working fine https://3v4l.org/12Hgc https://3v4l.org/AJSSB the both methods i recommend do the same job remove the extra back slashes from the domain name – ELMEHDI ACHAHED Jun 23 '23 at 00:31
  • Those demos don't use the original code you proposed (as per the [edit history](https://stackoverflow.com/posts/76536270/revisions) of this answer). But the question requested the brackets to be removed too, which you haven't achieved. And your second one doesn't even remove the quote marks, which was also requested. But there is no need for messing about with fiddly string functions anyway, as we have already proved- it's trivial to just decode the JSON and get the value. – ADyson Jun 23 '23 at 00:42
  • @ADyson I understand your concerns. While the provided demos may not align with the original code and specific requirements, I acknowledge the need to remove both the square brackets and quotation marks. In this case, a targeted string manipulation approach would be necessary. However, it's important to consider that JSON decoding can offer flexibility and robustness in other scenarios. Ultimately, the choice of approach depends on the project's context and requirements. Thank you for your input and highlighting the importance of addressing the specific needs mentioned in the question. – ELMEHDI ACHAHED Jun 23 '23 at 00:52
  • 1
    `In this case, a targeted string manipulation approach would be necessary`...no it wouldn't because we can just use json_decode, which already solves the problem with no effort and meets _all_ the requirements, not just some of them – ADyson Jun 23 '23 at 00:55
  • It wasn't my suggestion to use json_decode originally either- see the first comment on the main thread below the question, which I think was posted before you added this answer – ADyson Jun 23 '23 at 00:56
  • @ADyson I completely understand your point. The person who posted the problem mentioned their difficulties with str_replace, which is why I provided an alternative solution using the same str_replace . However, Sometimes different approaches work better for different situations, and it's great that we have multiple options to choose from. Thank you for pointing that out, and I appreciate your perspective. :) – ELMEHDI ACHAHED Jun 23 '23 at 01:01
  • They struggled with str_replace because it's simply the wrong tool for this job. – ADyson Jun 23 '23 at 01:03
  • @ADyson I also want to mention that I provided an answer despite of the one who post this problem . I understand that you may have more expertise in this area, and I appreciate their perspective. It's always valuable to learn from different viewpoints and approaches. Thank you for the discussion!" – ELMEHDI ACHAHED Jun 23 '23 at 01:04
  • 1
    @ADyson but as you see in the code i provided it did the job that remove the back slashes and quote marks from the url. – ELMEHDI ACHAHED Jun 23 '23 at 01:05
  • But not the brackets. And the technique is likely to fail in a more complex case - e.g. if the URL itself (perhaps in its query parameter values) legitimately contained any of those characters. Again...wrong tool for the job. The only foolproof way to get data, intact, from a JSON string is to use the ready made JSON functions which are designed to do exactly that. – ADyson Jun 23 '23 at 01:19
  • 1
    @ADyson I understand your point, and I appreciate the helpful conversation we had. It's true that using the JSON functions is a more robust approach for handling just strings, especially in cases where the data may contain characters that can interfere with string manipulation techniques. I will keep that in mind for future reference. Thank you for sharing your insights! – ELMEHDI ACHAHED Jun 23 '23 at 01:27