S
S
Sergey2017-10-28 18:26:44
PHP
Sergey, 2017-10-28 18:26:44

How to remove extra characters in phone link when displaying in WordPress (backend)?

Can you please tell me how to remove extra characters in the link to the phone when displaying in WordPress?
I am displaying the phone number via theme-options.php. The phone number contains spaces and the characters "(" and ")". And in order for the number in the link (href="tel:....") to be correct, these characters must not be there, so you need to cut them out with some function, tell me.

<a href="tel:<?php
  $options = get_option('sample_theme_options');
  echo $options['phonetext']; ?>">

  <?php
  $options = get_option('sample_theme_options');
  echo $options['phonetext']; ?>
</a>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Henry, 2017-10-28
@Henryh

Most reliable using regular expressions, for example using the preg_replace function

S
Sergey, 2017-10-28
@fenya-fl

Solved the problem this way:

<a href="tel:+<?php
  $options = get_option('sample_theme_options');
  $options = preg_replace('![^0-9]+!', '', $options);
  echo $options['phonetext']; ?>">

  <?php
  $options = get_option('sample_theme_options');
  echo $options['phonetext']; ?>
</a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question