1

I have a source code like this:

<script type='text/javascript' src='http://html.com/wp-content/plugins/contact-form-7/includes/js/scripts.js?ver=4.6'></script>
<script type='text/javascript'></script>
<script type='text/javascript' src='http://html.com/wp-content/plugins/table-of-contents-plus/front.min.js?ver=1509'></script>
<script type='text/javascript' src='http://html.com/wp-includes/js/wp-embed.min.js?ver=4.7'></script>

How can I replace ALL the src attributes with:

"http://www.example.com/site=PLACEHOLDER&somethingelse"? So I need to replace PLACEHOLDER with {the src website from above}

I found code snippets where I could replace the src link but not replacing it with a replaced url of itself.

How would one do that?

user754730
  • 1,341
  • 5
  • 31
  • 62

1 Answers1

1

Assuming you have the source code inside a variable you can do:

$code = "
<script type='text/javascript' src='http://html.com/wp-content/plugins/contact-form-7/includes/js/scripts.js?ver=4.6'></script>
<script type='text/javascript'></script>
<script type='text/javascript' src='http://html.com/wp-content/plugins/table-of-contents-plus/front.min.js?ver=1509'></script>
<script type='text/javascript' src='http://html.com/wp-includes/js/wp-embed.min.js?ver=4.7'></script>";

$pattern = "/src='([^']+)/i";
$replacement = "src='http://www.example.com/site=$1&somethingelse";

echo preg_replace($pattern, $replacement, $code);
Jose Garrido
  • 732
  • 1
  • 15
  • 31