0

I have the following code to remove a string if it contains the word "from" for each product on the page. This works well, but I only want to delete it one place for each product. (The string exists in two places for each product.)

$sub_notags = strip_tags( $subscription_string );

if ( strpos( $sub_notags, 'From:') !== false )  {
    return '';
}

Here's the HTML output of the place where I want it to work:

<label class="product_checkbox">Add <span class="price-string">From $68.00 on October 3rd each year</span></label>

And here's the place I want to ignore:

<span class="price"><span class="price-string">From $68.00 on October 3rd each year</span></span>

Update: I tried using strstr instead of strpos, because PHP docs said that should "find the first occurrence of a string". But that isn't working either. It is still affecting all occurrences.

Update2: Here is $subscription_string:

// translators: 1$: recurring amount, 2$: month (e.g. "March"), 3$: day of the month (e.g. "23rd") (e.g. "$15 on March 15th every 3rd year")

$subscription_string = sprintf( __( '%1$s on %2$s %3$s every %4$s year', 'woocommerce-subscriptions' ), $price, $wp_locale->month[ $payment_day['month'] ], WC_Subscriptions::append_numeral_suffix( $payment_day['day'] ), WC_Subscriptions::append_numeral_suffix( $billing_interval ) );
                            }

And here's a var_dump of $subscription_string:

string(199) $68.00 on October 3rd each year
LBF
  • 1,133
  • 2
  • 14
  • 39
  • http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php?s=1|9.9076 – AbraCadaver Oct 19 '16 at 18:37
  • what about $sub_notags = str_replace("Add From", "Add", $sub_notags);? – Stevish Oct 19 '16 at 18:41
  • Besides that, I would think you'd need to use preg_match or preg_replace – Stevish Oct 19 '16 at 18:41
  • I don't see any code in your question that replaces any text... Could you show the whole function? – Stevish Oct 19 '16 at 18:42
  • @Stevish, the code that deletes the whole string is `return ''` I tried str_replace but it affected both locations. – LBF Oct 19 '16 at 18:44
  • Not the best but `if (strpos($sub_notags, 'From:') !== false && strpos($sub_notags, 'Add') !== false) ` – AbraCadaver Oct 19 '16 at 18:50
  • 3
    strpos is NOT for manipulating html. it's for manipulating strings, regardless of what's in those strings. if you want to deal with html, use DOM. – Marc B Oct 19 '16 at 18:55
  • @AbraCadaver I thought that would work but "Add" is not part of the $sub_notags string. – LBF Oct 19 '16 at 18:59
  • @MarcB that makes sense, and I worded my question improperly. I was still hoping there was a way to target only the first occurrence of the string, rather than all occurrences. – LBF Oct 19 '16 at 19:00
  • Then your examples are wrong. – AbraCadaver Oct 19 '16 at 19:00
  • @AbraCadaver. Those are the real examples from my site. The word "Add" in included in the HTML. It is followed by $subscription_string, which includes the rest of the sentence. So code affecting $subscription_string does not affect the word "Add". Which is fine. I'd like to leave that word anyway. – LBF Oct 19 '16 at 19:12
  • I think we still need to see the whole function, and what's calling it. There's just something missing here. Is this function something that's called separately for each line? Is it called twice for each price? What are the entire contents of `$subscription_string`? – Stevish Oct 21 '16 at 19:45
  • @Stevish I've added the contents of $subscription_string. Does that shed any light? – LBF Oct 21 '16 at 22:42
  • Try explode() to solve your issue. It seems weird but its an ever best thing as regx – Aammad Ullah Oct 21 '16 at 22:50

0 Answers0