PHP - Output array value that contains a specific text marker - php

I'm using a function to output a collection of products in Shopify, onto a WordPress page.
I've got most of the data displaying correctly, except a custom value that is entered as a tag in Shopify. Using the api, I'm then trying to get this specific tag for the product subtitle which is formatted with att:Subtitle: before each product custom value/text.
This is the code I have got to (I've commented other unsuccessful attempts in the middle) - this is inside the overall code to show the 4 products in the Shopify collection:
// Using tags to output custom data from products
$tags = $current_product['tags'];
// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);
// Evaluate if there is a string with att:Subtitle in the product tags
// https://tecadmin.net/check-string-contains-substring-in-php/
$subtitle_attribute_key = "att:Subtitle:";
if (strpos($tags, 'att:Subtitle:') !== false) {
// Returns a numbered value corresponding to my subtitle attribute
// $key = array_search($subtitle_attribute_key, $product_tags);
$sub = strpos($tags, $subtitle_attribute_key);
// Turns the numbered value into a text value
// $numArray = explode(" ", $sub);
// var_dump($numArray);
$value = print_r($sub, true);
// $value = array_search("att:Subtitle:",$product_tabs);
// $value = array_search("att:Subtitle:", $tabs); // Warning: array_search() expects parameter 2 to be array, null given
// $result = $product_tags['$value']; // my attempt to return the text
// Remove att:Subtittle: in front of the subtitle value to output the clean final value
$subtitle = ltrim($value, 'att:Subtitle:');
}
So far it comes out as a number value where I'm displaying $subtitle... but I can't figure out how to display the custom text value instead.
Any ideas?
Thanks
EDIT: I am working with products that have multiple tags and I do not control these. Out of the tags I'm trying to find the one that starts with att:Subtitle: but only show the custom value after that marker.
When I echo $tags, the ist comes out something like this:
att:Benefit:balance, att:Perfect:Combination Skin, att:Size: 1.8 oz, att:Subtitle: Multi-Tasking Product, Key Ingredient 1, Key Ingredient 2, Essentials, meta-related-product-xyz, meta-related-product-brandname
They will all have a different list of tags

Of course it comes out as a number. You're getting a number from the strpos() and ultimate trying to run ltrim() on it.
If you're expecting the desired text to be in $tags immediately following 'att:Subtitle:' and have nothing else in that string, $subtitle = substr($tags, strpos('att:Subtitle:') + strlen('att:Subtitle:')); should give it to you. If you're expecting it to possibly be an element in the $product_tags array, need to loop (I'm assuming it shows up once, at most):
foreach ($product_tags as $product_tag) {
if (strpos($product_tag, 'att:Subtitle:') !== false) {
$subtitle = substr($product_tag, strpos($product_tag, 'att:Subtitle:') + strlen('att:Subtitle:'));
break;
}
}

So for what I understand you're trying to get rid of 'att:Subtitle:' from the tags on your products descriptions? maybe you should try replacing that value on your tags array
// Using tags to output custom data from products
$tags = $current_product['tags'];
// $tags is a string, this turns the values into an array
$product_tags = explode(',', $tags);
// Get rid of attribute on product tag
for($i = 0; $i < count($product_tags); $i++){
$subtitle = str_replace(':att:Subtitle','',$product_tags[i]);
}

Related

Trying to grab value from html page but getting template back not the value - php

I am making a price crawler for a project but am running into a bit of an issue. I am using the below code to extract values from an html page:
$content = file_get_contents($_POST['url']);
$resultsArray = array();
$sqlresult = array();
$priceElement = explode( '<div>value I want to extract</div>' , $content );
Now when I use this to get certain elements I only get back
Finance: {{value * value2}}
I want to get the actual value that would be displayed on the screen e.g
Finance: 7.96
The other php methods I have tried are:
curl
file_get_html(using simple_html_dom library)
None of these work either :( Any ideas what I can do?
You just set the <div>value I want to extract</div> as a delimiter, which means PHP looks for it to separate your string to array whenever this occurs.
In the following code we use , character as a delimiter:
<?php
$string = "apple,banana,lemon";
$array = explode(',', $string);
echo $array[1];
?>
The output should be this:
banana
In your example you set the value you want to extract as a delimiter. That's why this happens to you. You'll need to set a delimiter between your string you want to obtain and other string you won't need at the moment.
For example:
<?php
$string = "iDontNeedThis-dontExtractNow-value I want to extract-dontNeedEither";
$priceElement = explode('-', $string);
echo "<div>".$priceElement[2]."</div>";
?>
The code should output this to your HTML page:
<div>value I want to extract</div>
And it will appear on your page like this:
value I want to extract
If you don't need to save the whole array in a variable, you can save the one index of it to variable instead:
$priceElement = explode('-', $string)[2];
echo $priceElement;
This will save only value I want to extract so you won't have to deal with arrays later on.

hide certain category from single post

I need to limit number of categories display in a single post on wordpress using this script
function swift_list_cats($num){
$temp=get_the_category();
$count=count($temp);// Getting the total number of categories the post is filed in.
for($i=0;$i<$num&&$i<$count;$i++){
//Formatting our output.
$cat_string.=''.$temp[$i]->cat_name.'';
if($i!=$num-1&&$i+1<$count)
//Adding a ',' if it’s not the last category.
//You can add your own separator here.
$cat_string.=' | ';
}
echo $cat_string;
}
But I also need to hide some categories. How would I do that?
Simple answer is to return a new count not based on the length of your array so
for($i=0;$i<$num&&$i<$count;$i++){
becomes:
for($i=0;$i<10;$i++){
or use $temp = array_slice(get_the_category(), //ofset 0, //length 10);
Array slice info can be seen here.
If you need to hide some values, this would be far easier to do by adding a hidden column to your database and select values WHERE hidden = 0. However to address your question.You need to know which values you want to hide. Store these in an array and do the following.
function swift_list_cats($num){
$hiddenArray = [12, 105];
//change offset and limit to what you desire removing the slashes and labels
$temp = array_slice(get_the_category(), 0, 10);
$count=count($temp);
$cat_string = '';
for($i=0;$i<$num&&$i<$count;$i++){
if(in_array($temp[i]->cat_ID, $hiddenArray)){
continue;
}
$cat_string.=''.$temp[$i]->cat_name.'';
if($i!=$num-1&&$i+1<$count)
$cat_string.=' | ';
}
echo $cat_string;
}
$hiddenArray is the array you populate with your category IDs that you want hidden

Woocommerce Order / AFC get_sub_field / get_sub_field_object (array)

Have setup ACF repeater field that stores various amount of tracking numbers in the order. Having 0 success with retrieving this information so need some advice.
Am using this to put information in subfields and it does the job
foreach ($base->DocumentLines->DocumentLine as $item) {
foreach ($item->MiscData as $misc) {
foreach ($misc->PackageNo as $package) {
$trackno = (string)$package->TrackingNo;
update_post_meta("$order_id", $field_rep, $count);
$sub = $count +1;
update_sub_field(array($field_key_rep, $sub, $field_key_sub), $trackno, "$order_id");
$count = $count + 1;
update_field($field_key, $trackno, "$order_id");
}
}
}
This works well, but then i need to retrieve this numbers and write em out. They are getting included in an email so need to retrieve the data outside of order.
Before rebuilding the function to be able to handle multiple numbers i did use a single field and could retrieve the information with
get_post_meta($order_id, 'tracking', true);
Feels like i have been trying everything now but got absolutely nothing.
Image from one of the orders, in this one it’s 10 tracking numbers but it varies from 1 to 20 if it's to any help.
The feeling when you realize after 10 hours that your missed a capital letter in sub field name.
Just wanted to submit my solution and hopefully it can help someone else having issues with ACF Repeater fields + Woocommerce
For my specific case i did make a function that could extract all the tracking numbers my function above did add from XML files.
$function trackingNo($postID) {
$field_rep = 'trackingNo';
$field_sub = 'no';
if (have_rows($field_rep, $postID)) {
$trackingNo = array();
// loop through the rows of data
while (have_rows($field_rep, $postID)):
the_row();
// Add to array
$trackingNo[] = get_sub_field($field_sub);
endwhile;
$foo = implode('&consignmentId=', $trackingNo);
$bar = 'urlzz/tracktrace/TrackConsignments_do.jsp?&consignmentId=';
$value = $bar . $foo;
return $value;
}
}
Any advice for improvement is always welcome, my PHP is so'n'so :)

Parsing a single XML section using a query string with PHP

I'm trying to retrieve a single section from an XML file using PHP.
Here's the code I'm using:
<?php
$articles = simplexml_load_file('articles.xml');
foreach ($articles as $articlecontent)
{
$title = $articlecontent->title;
$content = $articlecontent->content;
$author = $articlecontent->author;
$date = $articlecontent['date'];
echo "<h1>",$title,"</h1>\n",
"<p><i>",$date,"</i></p>\n",
"<p>",$content,"</p>\n",
"<p>",$author,"</p>\n",
"<p><time>",$date,"</time></p>\n"
;
}
?>
Which shows all the sections in the XML file, but how can I parse just one result but do so using a query string?
Example: articles.php?title=section-one will retrieve the XML section with the title "section-one"
You get the title from the query string by getting it from PHP's superglobal $_GET array, like this:
$title = false;
if (isset($_GET['title']))
{
$title = $_GET['title'];
}
and then the simplest way would be to compare it to the title from the XML file in your foreach loop.
Way to get title from url is using $_GET array, then to get required article by title i think you could use xpath which won't require foreach loop
$title = $_GET['title'];
// there should go some sanitizing
$articles = simplexml_load_file('articles.xml');
//search for article by title
//assuming that xml root tag is articles and article tag is article and searching by attribute named title
$foundArticles = $articles->xpath('articles/article[#title='.$title.']');
//query depends on xml structure
// now you have array of matched articles with matching title from url
foreach($foundArticles as $singleArticle)
{
//do printing here
}
This code is not tested, but principle should work.
To receive Query String Variable (in our case 'title') from URL you can use $_REQUEST also. Little bit corrected code (#user1597483) with some more advanced updates.
$articles = simplexml_load_file('articles.xml');
$title = "";
if(isset($_REQUEST['title'])) //receiving Query String from URL
$title = $_REQUEST['title'];
if(count($articles)):
//Fixed search means that will one return article if 'section-one' in title
//$result = $articles->xpath("//article[title='Section-one']");
//Match All Search means that will return all articles that contains 'section-one' in title
$result = $articles->xpath("//article[contains(translate(title, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'".strtolower($title)."')]");
foreach ($result as $articleItem):
$title=$articleItem->title;
$content=$articleItem->content;
$author=$articleItem->author;
$date=$articleItem->date;
echo "<h1>",$title,"</h1>\n";
echo "<p><i>",$date,"</i></p>\n";
echo "<p>",$content,"</p>\n";
echo "<p>",$author,"</p>\n";
echo "<p><time>",$date,"</time></p>\n";
endforeach;
endif;
In the code above I have specified two types of filter through which you can get result with Specific Article by exact match or wild card type matching by contains function. And I have also used translate function which will convert value of title element to lowercase and also converted value of Query String Variable to lowercase. So that regardless of Upper/Lower case, condition will be checking with lowercase and if condition matched then resultant articles will be returned.
You can achieve that by inserting a condition into the foreach loop testing if the title-filter is in action and does not match. If so, skip that entry by using the continue keyword:
$titleFilter = trim(isset($_GET['title']) ? $_GET['title'] : '');
...
foreach ($articles as $articlecontent)
{
$title = $articlecontent->title;
if (strlen($titleFilter) and $title !== $titleFilter) {
continue;
}
...

php select specific content from input file to put in array elements

I have an input file (exert from file shown below) with multiple lines that I need to select specific text from and put each selection into an array element:
exert from input file:
"BLOCK","PARTNO"
"ELEMENT","HEADER-"
"NAME","1AB000072186"
"REVISION","0000"
"PARTSHAPE","RECT_074_044_030"
"PACKAGE","120830E"
"PMABAR",""
"PARTCOMMENT","CAP-TANT*150uF*20%*10V7343*4.3mm"
"ELEMENT","PRTIDDT-"
"PMAPP",1
"PMADC",2
"ComponentQty",2
"BLOCK","PARTNO"
"ELEMENT","HEADER-"
"NAME","1AB030430005"
"REVISION","0000"
"PARTSHAPE","RECT_072_042_030"
"PACKAGE","120830E"
"PMABAR",""
"PARTCOMMENT","1.0000 Amp SUBMINIATURE FUSE"
"ELEMENT","PRTIDDT-"
"PMAPP",2
"PMADC",0
"ComponentQty",1
"BLOCK","PARTNO"
"ELEMENT","HEADER-"
"NAME","1AB030430001"
"REVISION","0000"
"PARTSHAPE","RECT_072_042_030"
"PACKAGE","120830E"
"PMABAR",""
"PARTCOMMENT","2.0000 Amp SUBMINIATURE FUSE"
"ELEMENT","PRTIDDT-"
"PMAPP",2
"PMADC",0
"ComponentQty",1
Notice that after each occurrence of the line with the phrase "ComponentQty" the content begins repeating...
Where I need the PartNumber that is next to the occurrence of "NAME" in one dimension of the array element and the content next to the occurrence of "PARTSHAPE" in the second dimension for each element. I am very confused on how to do this though...please help!!!
$fh = fopen('yourfile.txt', 'rb');
$found_stuff = array();
$last_component = null;
while($line = fgets($fh)) { // read a line
$parts = explode(',', $line); // split into components
switch($parts[0]) { // based on which key we're on
case '"NAME"':
$last_component = $parts[1]; // save the key's value
break;
case '"PARTSHAPE"':
$found_stuff[$last_component] = $parts[1]; // store the partshape name
break;
}
}
fclose($fh);
This should do the basic work. Read a line, explode it into pieces where commas occur. The first part will be the "key", the second part will be the value. Then simply keep reading until we either hit a NAME or a PARTSHAPE key, then store the values as appropriate.
Note that I've not stripped the double-quotes off the values. That's left as an exercise to the reader. This code also assumes that the file's format is regular and that a "NAME" will show up before any PARTSHAPE lines, and there'll be a perfect 1:1 alternation between NAME/PARTSHAPE lines. If you get two PARTSHAPES in a row, you'll lose the first one. And if a PARTSHAPE shows up before the first NAME is encounted, you'll sorta lose that one too.
The following steps worked for me:
The section pasted in my OP (repeating many times more) is defined as $PartNoContents
and $BlockData[] is the array that I need to paste selections from $PartNoContents into.
$PartNoContents = str_replace('"', '', $PartNoContents);
$PartLines = explode("\n", $PartNoContents);
$PartData = array();
foreach ($PartLines as $PartLine){
$PartData[] = explode(',', $PartLine);
}
for($p=0;$p<count($PartLines);$p++){
if ( isset( $PartData[$p][1] ) && !empty( $PartData[$p][1] ) ){
$p1 = str_replace(chr(13), '', $PartData[$p][1]);
if ( isset($BlockData[$b][0]) && !empty($BlockData[$b][0]) && $BlockData[$b][7]==$p1 ){
$BlockData[$b][13] = str_replace(chr(13), '', $PartData[$p+$PartDataIncNum][1]);
$p = count($PartLines) ;
}
}
}

Categories