Friday, January 6, 2023
HomeWordPress Developmentphp - Strings and Arrays - what am I lacking?

php – Strings and Arrays – what am I lacking?


Fast premise: I have never labored in PHP for over 15 years. So possibly, what I am lacking, is one thing very primary 😉

The problem:

I am working with a WordPress plugin. This plugin creates an add discipline. As soon as I take advantage of it to add a file, it returns an array containing a listing of the URLs of the recordsdata uploaded.

To get a particular URL I do one thing like this: $params[‘fieldname2’][0]

Now, when I attempt to save immediately the document 0 to the DB, every thing works high-quality and I get a clear URL saved.

Nevertheless, I want this similar document to do different 3 issues.

#1: copy a particular document to a variable

To unravel #2 and #3 my first concept was to make a easy copy of the variable. One thing like:
$cleanurl = $params[‘fieldname2’][0];
However, if I do that, $cleanurl stays empty.

#2: examine if the document [0] is empty

To do it I take advantage of if(strlen($param[‘fieldname4_url’][0]) === 0) . It all the time returns that the document equals to 0. Nevertheless, if I do that: if(strlen(‘https://exampleurl.com/picture.jpg’ ) === 0) (which is similar sort of string the array incorporates) the if works appropriately and goes into the else situation.

So, what am I lacking right here?

#3: utilizing the url inside a perform

WordPress has this perform: attachment_url_to_postid(). It solely wants a string url as a parameter in an effort to return a quantity. If I do that:

$imageid = attachment_url_to_postid($param[‘fieldname4_url’][0]);

I get nothing. If I do:

$imageid = attachment_url_to_postid(‘https://exampleurl.com/picture.jpg’);

I get the right quantity.

Once more, what do you suppose I’m lacking right here?

Thanks on your time 🙂

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments